Skip to content

Commit 312c1d1

Browse files
committed
feat: keep login status
1 parent 9615264 commit 312c1d1

File tree

13 files changed

+55
-59
lines changed

13 files changed

+55
-59
lines changed

api/entrance.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ func AppRun() {
1616

1717
r.Use(gin.RecoveryWithWriter(&recoverWriter{}))
1818
r.Use(middleware.JSONRequestContextHandler(func(c *gin.Context) model.APIRequest {
19-
if c.Request.URL.Path == "/api/register" {
19+
if c.Request.URL.Path == "/api/user/register" {
2020
return new(model.RegisterRequest)
21-
} else if c.Request.URL.Path == "/api/login" {
21+
} else if c.Request.URL.Path == "/api/user/login" {
2222
return new(model.LoginRequest)
2323
} else if c.Request.URL.Path == "/api/topics" {
2424
return new(model.ArticleRequest)
@@ -38,9 +38,10 @@ func AppRun() {
3838
user.GET("/configs", guest.GetConfigs)
3939

4040
// user.Use()
41-
user.POST("/register", guest.RegisterByEmail)
42-
user.POST("/login", guest.Login)
43-
user.GET("/currentUser", guest.GetCurrentUser)
41+
user.POST("/user/register", guest.RegisterByEmail)
42+
user.POST("/user/login", guest.Login)
43+
user.POST("/user/logout", guest.Logout)
44+
user.GET("/user/current", guest.GetCurrentUser)
4445

4546
user.POST("/topic", guest.PostArticle)
4647
user.GET("/topics", guest.GetArticleList)

api/guest/users.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import (
1010

1111
// GetCurrentUser ...
1212
func GetCurrentUser(c *gin.Context) {
13-
service.UserService.GetCurrentUser(c)
13+
user := service.UserService.GetCurrentUser(c)
14+
setAPIResponse(c, user, "", true)
1415
}
1516

1617
// RegisterByEmail ...
@@ -30,11 +31,16 @@ func Login(c *gin.Context) {
3031
setAPIResponse(c, nil, err.Error(), false)
3132
} else {
3233
token := service.UserService.SetToken(user.ID)
33-
value := model.NewResponseValue().Set("token", token).Set("user", user)
34-
setAPIResponse(c, value, "登录成功", true)
34+
data := model.NewResponseValue().Set("token", token).Set("user", user)
35+
setAPIResponse(c, data.Value, "登录成功", true)
3536
}
3637
}
3738

39+
// Logout ...
40+
func Logout(c *gin.Context) {
41+
42+
}
43+
3844
// TestForUser is the test api for user
3945
func TestForUser(c *gin.Context) {
4046
fmt.Println(c.Request.URL.Query())

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/mitchellh/mapstructure v1.3.3 // indirect
1313
github.com/natefinch/lumberjack v2.0.0+incompatible
1414
github.com/pelletier/go-toml v1.8.1 // indirect
15-
github.com/satori/go.uuid v1.2.0 // indirect
15+
github.com/satori/go.uuid v1.2.0
1616
github.com/spf13/afero v1.4.0 // indirect
1717
github.com/spf13/cast v1.3.1 // indirect
1818
github.com/spf13/jwalterweatherman v1.1.0 // indirect

model/model.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package model
22

33
// Models stores the models which will be create as tables in the mysql
4-
var Models = []interface{}{&User{}, &Article{}, &Comment{}, &UserLikeArticle{}}
4+
var Models = []interface{}{&User{}, &Article{}, &Comment{}, &UserLikeArticle{}, &UserToken{}}
55

66
// Model ...
77
type Model struct {
@@ -12,7 +12,7 @@ type Model struct {
1212
type User struct {
1313
Model
1414
Username string `gorm:"column:username;type:varchar(20);unique;not null" json:"username"`
15-
Nickname string `gorm:"column:nickname;type:varchar(30)" json:"nick_name"`
15+
Nickname string `gorm:"column:nickname;type:varchar(30)" json:"nickname"`
1616
Password string `gorm:"column:password;type:varchar(100);not null" json:"password"`
1717
AvatarURL string `gorm:"column:avatar_url;type:varchar(200)" json:"avatar_url"`
1818
Gender string `gorm:"column:gender;type:tinyint;default:2" json:"gender"`
@@ -37,7 +37,7 @@ type User struct {
3737
type UserToken struct {
3838
Model
3939
UserID int64 `gorm:"column:user_id;type:int" json:"user_id"`
40-
Token string `gorm:"size:32;unique;not null" json:"token"`
40+
Token string `gorm:"type:varchar(40);unique;not null" json:"token"`
4141
ExpiredAt int64 `gorm:"column:expired_at;type:int" json:"expired_at"`
4242
CreateTime int64 `gorm:"column:create_time;default:null" json:"create_time"`
4343
}

repository/token_repo.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ func (r *userTokenRepository) Create(db *gorm.DB, userToken *model.UserToken) er
2020
return db.Create(userToken).Error
2121
}
2222

23+
func (r *userTokenRepository) DeleteByToken(db *gorm.DB, token string) error {
24+
return db.Where("token = ?", token).Delete(&model.UserToken{}).Error
25+
}
26+
2327
func (r *userTokenRepository) GetUserIDByToken(db *gorm.DB, token string) (*model.UserToken, error) {
28+
if token == "" {
29+
return nil, nil
30+
}
2431
return r.take(db, "token = ?", token)
2532
}
2633

service/users.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ func (s *userService) loginByUsername(username string, password string) (*model.
139139
return user, nil
140140
}
141141

142+
func (s *userService) Logout(c *gin.Context) error {
143+
token := s.GetToken(c)
144+
return repository.UserTokenRepository.DeleteByToken(util.DB(), token)
145+
}
146+
142147
func (s *userService) SetToken(userID int64) string {
143148
token := uuid.NewV4().String()
144149
expireTime := time.Now().Add(time.Hour * 24 * time.Duration(model.TokenExpireDays))

site/components/MyNav.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export default {
9191
},
9292
computed: {
9393
user() {
94+
console.log(this.$store.state.user.current)
9495
return this.$store.state.user.current
9596
},
9697
isOwnerOrAdmin() {

site/package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

site/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"@nuxt/types": "^2.14.7",
1717
"@nuxtjs/axios": "^5.12.5",
1818
"@nuxtjs/google-adsense": "^1.1.3",
19+
"JSON": "^1.0.0",
1920
"cookie-universal-nuxt": "^2.1.4",
2021
"element-ui": "^2.13.2",
2122
"nuxt": "^2.14.1",

site/pages/index.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export default {
3030
async asyncData({ $axios, params }) {
3131
try {
3232
const [topicsPage] = await Promise.all([$axios.get('/api/topics')])
33-
console.log(topicsPage)
3433
return { topicsPage }
3534
} catch (e) {
3635
console.error(e)

0 commit comments

Comments
 (0)