Implement login resolver (#15)
* add sign_up_method to users table * add session store * implement login resolver
This commit is contained in:
39
server/utils/authToken.go
Normal file
39
server/utils/authToken.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt"
|
||||
"github.com/yauthdev/yauth/server/constants"
|
||||
"github.com/yauthdev/yauth/server/enum"
|
||||
)
|
||||
|
||||
type UserAuthInfo struct {
|
||||
Email string `json:"email"`
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
type UserAuthClaim struct {
|
||||
*jwt.StandardClaims
|
||||
TokenType string `json:"token_type"`
|
||||
UserAuthInfo
|
||||
}
|
||||
|
||||
func CreateAuthToken(user UserAuthInfo, tokenType enum.TokenType) (string, error) {
|
||||
t := jwt.New(jwt.GetSigningMethod(constants.JWT_TYPE))
|
||||
expiryBound := time.Hour
|
||||
if tokenType == enum.RefreshToken {
|
||||
// expires in 90 days
|
||||
expiryBound = time.Hour * 2160
|
||||
}
|
||||
|
||||
t.Claims = &UserAuthClaim{
|
||||
&jwt.StandardClaims{
|
||||
ExpiresAt: time.Now().Add(expiryBound).Unix(),
|
||||
},
|
||||
tokenType.String(),
|
||||
user,
|
||||
}
|
||||
|
||||
return t.SignedString([]byte(constants.JWT_SECRET))
|
||||
}
|
17
server/utils/cookie.go
Normal file
17
server/utils/cookie.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/yauthdev/yauth/server/constants"
|
||||
)
|
||||
|
||||
func SetCookie(gc *gin.Context, token string) {
|
||||
secure := true
|
||||
httpOnly := true
|
||||
|
||||
if !constants.IS_PROD {
|
||||
secure = false
|
||||
}
|
||||
|
||||
gc.SetCookie(constants.COOKIE_NAME, token, 3600, "/", GetFrontendHost(), secure, httpOnly)
|
||||
}
|
23
server/utils/ginContext.go
Normal file
23
server/utils/ginContext.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func GinContextFromContext(ctx context.Context) (*gin.Context, error) {
|
||||
ginContext := ctx.Value("GinContextKey")
|
||||
if ginContext == nil {
|
||||
err := fmt.Errorf("could not retrieve gin.Context")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
gc, ok := ginContext.(*gin.Context)
|
||||
if !ok {
|
||||
err := fmt.Errorf("gin.Context has wrong type")
|
||||
return nil, err
|
||||
}
|
||||
return gc, nil
|
||||
}
|
16
server/utils/urls.go
Normal file
16
server/utils/urls.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"github.com/yauthdev/yauth/server/constants"
|
||||
)
|
||||
|
||||
func GetFrontendHost() string {
|
||||
u, err := url.Parse(constants.FRONTEND_URL)
|
||||
if err != nil {
|
||||
return `localhost`
|
||||
}
|
||||
|
||||
return u.Hostname()
|
||||
}
|
Reference in New Issue
Block a user