79 lines
2.0 KiB
Go
79 lines
2.0 KiB
Go
![]() |
package resolvers
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/yauthdev/yauth/server/db"
|
||
|
"github.com/yauthdev/yauth/server/enum"
|
||
|
"github.com/yauthdev/yauth/server/graph/model"
|
||
|
"github.com/yauthdev/yauth/server/session"
|
||
|
"github.com/yauthdev/yauth/server/utils"
|
||
|
"golang.org/x/crypto/bcrypt"
|
||
|
)
|
||
|
|
||
|
func Login(ctx context.Context, params model.LoginInput) (*model.LoginResponse, error) {
|
||
|
gc, err := utils.GinContextFromContext(ctx)
|
||
|
var res *model.LoginResponse
|
||
|
if err != nil {
|
||
|
return res, err
|
||
|
}
|
||
|
|
||
|
params.Email = strings.ToLower(params.Email)
|
||
|
user, err := db.Mgr.GetUserByEmail(params.Email)
|
||
|
if err != nil {
|
||
|
return res, errors.New(`User with this email not found`)
|
||
|
}
|
||
|
|
||
|
if !strings.Contains(user.SignupMethod, enum.BasicAuth.String()) {
|
||
|
return res, errors.New(`User has not signed up email & password`)
|
||
|
}
|
||
|
|
||
|
if user.EmailVerifiedAt <= 0 {
|
||
|
return res, errors.New(`Email not verified`)
|
||
|
}
|
||
|
// match password
|
||
|
cost, err := bcrypt.Cost([]byte(user.Password))
|
||
|
log.Println(cost, err)
|
||
|
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(params.Password))
|
||
|
|
||
|
if err != nil {
|
||
|
log.Println("Compare password error:", err)
|
||
|
return res, errors.New(`Invalid Password`)
|
||
|
}
|
||
|
userIdStr := fmt.Sprintf("%d", user.ID)
|
||
|
refreshToken, _, _ := utils.CreateAuthToken(utils.UserAuthInfo{
|
||
|
ID: userIdStr,
|
||
|
Email: user.Email,
|
||
|
}, enum.RefreshToken)
|
||
|
|
||
|
accessToken, expiresAt, _ := utils.CreateAuthToken(utils.UserAuthInfo{
|
||
|
ID: userIdStr,
|
||
|
Email: user.Email,
|
||
|
}, enum.AccessToken)
|
||
|
|
||
|
session.SetToken(userIdStr, refreshToken)
|
||
|
|
||
|
res = &model.LoginResponse{
|
||
|
Message: `Logged in successfully`,
|
||
|
AccessToken: &accessToken,
|
||
|
AccessTokenExpiresAt: &expiresAt,
|
||
|
User: &model.User{
|
||
|
ID: userIdStr,
|
||
|
Email: user.Email,
|
||
|
Image: &user.Image,
|
||
|
FirstName: &user.FirstName,
|
||
|
LastName: &user.LastName,
|
||
|
SignupMethod: user.SignupMethod,
|
||
|
EmailVerifiedAt: &user.EmailVerifiedAt,
|
||
|
},
|
||
|
}
|
||
|
|
||
|
utils.SetCookie(gc, accessToken)
|
||
|
|
||
|
return res, nil
|
||
|
}
|