| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- // Package auth provides authentication and authorization functionality.
- package auth
- import (
- "errors"
- "github.com/gin-gonic/gin"
- "github.com/runningwater/gohub/app/models/user"
- "github.com/runningwater/gohub/pkg/logger"
- )
- // Attemp login
- func Attemp(email, password string) (user.User, error) {
- userModel := user.GetByMulti(email)
- if userModel.ID == 0 {
- return user.User{}, errors.New("账号不存在")
- }
- if !userModel.ComparePassword(password) {
- return user.User{}, errors.New("密码错误")
- }
- return userModel, nil
- }
- // LoginByPhone 登陆指定用户
- func LoginByPhone(phone string) (user.User, error) {
- userModel := user.GetByPhone(phone)
- if userModel.ID == 0 {
- return user.User{}, errors.New("账号不存在")
- }
- return userModel, nil
- }
- // CurrentUser 获取当前用户
- func CurrentUser(c *gin.Context) user.User {
- userModel, ok := c.MustGet("current_user").(user.User)
- if !ok {
- logger.LogIf(errors.New("无法获取用户"))
- return user.User{}
- }
- // db is now a *gorm.DB object
- return userModel
- }
- // CurrentUID 获取当前用户 ID
- func CurrentUID(c *gin.Context) string {
- return c.GetString("current_user_id")
- }
|