Compare commits
No commits in common. "dbf84701952f8674a45378cf1ec7c97a911e7516" and "0bfb2e6e86edd18f1a9d88be99479bdb2a064942" have entirely different histories.
dbf8470195
...
0bfb2e6e86
@ -36,7 +36,18 @@ func GoogleCallback(ctx *gin.Context) {
|
|||||||
if jwt, dbUser, googleUserInfo, err := deps.AuthService.GoogleAuthSuccess(state, code); err != nil {
|
if jwt, dbUser, googleUserInfo, err := deps.AuthService.GoogleAuthSuccess(state, code); err != nil {
|
||||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
} else {
|
} else {
|
||||||
|
// TODO: Update these values when using a real domain. Maybe an ENV?
|
||||||
domain.SetCookie(ctx, "jwt_token", jwt, time.Hour*24*7)
|
domain.SetCookie(ctx, "jwt_token", jwt, time.Hour*24*7)
|
||||||
|
// ctx.SetCookie(
|
||||||
|
// "jwt_token",
|
||||||
|
// jwt,
|
||||||
|
// int(time.Now().Add(7*24*time.Hour).Sub(time.Now()).Seconds()),
|
||||||
|
// "/",
|
||||||
|
// "", // TODO: Real live domain
|
||||||
|
// false, // TODO: True in prod
|
||||||
|
// true,
|
||||||
|
// )
|
||||||
|
|
||||||
// ctx.JSON(http.StatusOK, gin.H{"jwt": jwt, "googleUserInfo": googleUserInfo, "dbUser": dbUser})
|
// ctx.JSON(http.StatusOK, gin.H{"jwt": jwt, "googleUserInfo": googleUserInfo, "dbUser": dbUser})
|
||||||
_ = dbUser
|
_ = dbUser
|
||||||
_ = googleUserInfo
|
_ = googleUserInfo
|
||||||
@ -49,7 +60,11 @@ func GoogleCallback(ctx *gin.Context) {
|
|||||||
// require authentication will require the user to sign back in before accessing them again.
|
// require authentication will require the user to sign back in before accessing them again.
|
||||||
// This route will direct the user back to the home page.
|
// This route will direct the user back to the home page.
|
||||||
func Logout(ctx *gin.Context) {
|
func Logout(ctx *gin.Context) {
|
||||||
|
// TODO: Use same values as the GoogleCallback function
|
||||||
domain.SetCookie(ctx, "jwt_token", "", -1)
|
domain.SetCookie(ctx, "jwt_token", "", -1)
|
||||||
domain.SetCookie(ctx, "search-filters", "", -1)
|
domain.SetCookie(ctx, "search-filters", "", -1)
|
||||||
|
// ctx.SetCookie("jwt_token", "", -1, "/", "", false, true) // TODO: Update settings
|
||||||
|
// ctx.SetCookie("search-filters", "", -1, "/", "", false, true)
|
||||||
|
|
||||||
ctx.Redirect(http.StatusSeeOther, domain.WEB_HOME)
|
ctx.Redirect(http.StatusSeeOther, domain.WEB_HOME)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -28,14 +28,6 @@ func HomePage(ctx *gin.Context) {
|
|||||||
|
|
||||||
loggedIn := domain.IsLoggedIn(ctx)
|
loggedIn := domain.IsLoggedIn(ctx)
|
||||||
|
|
||||||
// Ensure user is logged in with a valid account
|
|
||||||
if user := deps.UserService.GetAuthenicatedUser(ctx); user == nil {
|
|
||||||
// Log (stale) user out
|
|
||||||
domain.SetCookie(ctx, "jwt_token", "", -1)
|
|
||||||
domain.SetCookie(ctx, "search-filters", "", -1)
|
|
||||||
loggedIn = false
|
|
||||||
}
|
|
||||||
|
|
||||||
var page templ.Component
|
var page templ.Component
|
||||||
if loggedIn {
|
if loggedIn {
|
||||||
userId := ctx.MustGet("userId").(int)
|
userId := ctx.MustGet("userId").(int)
|
||||||
@ -158,12 +150,6 @@ func ProfilePage(ctx *gin.Context) {
|
|||||||
// Else, get the user data
|
// Else, get the user data
|
||||||
deps := ctx.MustGet("deps").(*domainServer.InjectedDependencies)
|
deps := ctx.MustGet("deps").(*domainServer.InjectedDependencies)
|
||||||
user := deps.UserService.GetAuthenicatedUser(ctx)
|
user := deps.UserService.GetAuthenicatedUser(ctx)
|
||||||
if user == nil {
|
|
||||||
// User is failing to be found, direct to the login page
|
|
||||||
ctx.Redirect(http.StatusSeeOther, domainServer.WEB_LOGIN)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
recipes, err := deps.RecipeService.GetUserRecipes(user.Id)
|
recipes, err := deps.RecipeService.GetUserRecipes(user.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.JSON(http.StatusInternalServerError, gin.H{
|
ctx.JSON(http.StatusInternalServerError, gin.H{
|
||||||
@ -193,7 +179,7 @@ func ProfilePage(ctx *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
title := "Potion - Profile"
|
title := "Potion - Profile"
|
||||||
page := pages.ProfilePage(*user, recipes, favorites, engagements)
|
page := pages.ProfilePage(user, recipes, favorites, engagements)
|
||||||
|
|
||||||
ctx.HTML(http.StatusOK, "", layouts.AppLayout(title, page))
|
ctx.HTML(http.StatusOK, "", layouts.AppLayout(title, page))
|
||||||
}
|
}
|
||||||
@ -222,15 +208,6 @@ func RecipePage(ctx *gin.Context) {
|
|||||||
// Get signed in user, if they exist
|
// Get signed in user, if they exist
|
||||||
var userId *int = nil
|
var userId *int = nil
|
||||||
var loggedIn = domainServer.IsLoggedIn(ctx)
|
var loggedIn = domainServer.IsLoggedIn(ctx)
|
||||||
|
|
||||||
// Ensure user is logged in with a valid account
|
|
||||||
if user := deps.UserService.GetAuthenicatedUser(ctx); user == nil {
|
|
||||||
// Log (stale) user out
|
|
||||||
domain.SetCookie(ctx, "jwt_token", "", -1)
|
|
||||||
domain.SetCookie(ctx, "search-filters", "", -1)
|
|
||||||
loggedIn = false
|
|
||||||
}
|
|
||||||
|
|
||||||
if loggedIn {
|
if loggedIn {
|
||||||
storeId := ctx.MustGet("userId").(int)
|
storeId := ctx.MustGet("userId").(int)
|
||||||
userId = &storeId
|
userId = &storeId
|
||||||
|
|||||||
@ -25,19 +25,19 @@ func NewUserService(userRepository domain.UserRepository) domain.UserService {
|
|||||||
// user is actually logged in, if not, a blank user will be returned. To ensure success, call the
|
// user is actually logged in, if not, a blank user will be returned. To ensure success, call the
|
||||||
// `domain.IsLoggedIn()` function first to ensure the user is logged in. If that passes, this
|
// `domain.IsLoggedIn()` function first to ensure the user is logged in. If that passes, this
|
||||||
// function should yield a result.
|
// function should yield a result.
|
||||||
func (s *UserService) GetAuthenicatedUser(ctx *gin.Context) *domain.User {
|
func (s *UserService) GetAuthenicatedUser(ctx *gin.Context) domain.User {
|
||||||
val, ok := ctx.Get("userId")
|
val, ok := ctx.Get("userId")
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return domain.User{}
|
||||||
}
|
}
|
||||||
|
|
||||||
id := val.(int)
|
id := val.(int)
|
||||||
user, err := s.userRepository.GetUser(id)
|
user, err := s.userRepository.GetUser(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return domain.User{}
|
||||||
}
|
}
|
||||||
|
|
||||||
return user
|
return *user
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUser will get a user from the database via its ID. This is not related to the Google ID in
|
// GetUser will get a user from the database via its ID. This is not related to the Google ID in
|
||||||
|
|||||||
@ -3,6 +3,6 @@ package domain
|
|||||||
import "github.com/gin-gonic/gin"
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
type UserService interface {
|
type UserService interface {
|
||||||
GetAuthenicatedUser(ctx *gin.Context) *User
|
GetAuthenicatedUser(ctx *gin.Context) User
|
||||||
GetUser(id int) (*User, error)
|
GetUser(id int) (*User, error)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user