2018-08-28 03:55:38 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.trj.tw/golang/mtfosbot/model"
|
2018-09-16 15:39:15 +00:00
|
|
|
"git.trj.tw/golang/mtfosbot/module/apis/twitch"
|
2018-08-28 03:55:38 +00:00
|
|
|
"git.trj.tw/golang/mtfosbot/module/context"
|
2018-09-02 01:15:27 +00:00
|
|
|
"github.com/gin-gonic/contrib/sessions"
|
2018-08-28 03:55:38 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
)
|
|
|
|
|
2018-09-16 15:39:15 +00:00
|
|
|
// CheckSession -
|
|
|
|
func CheckSession(c *context.Context) {
|
|
|
|
session := sessions.Default(c.Context)
|
|
|
|
userData := session.Get("user")
|
|
|
|
loginType := session.Get("loginType")
|
|
|
|
if userData == nil || loginType == nil {
|
|
|
|
c.LoginFirst(nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var name string
|
|
|
|
var ltype string
|
|
|
|
var ok bool
|
|
|
|
switch userData.(type) {
|
|
|
|
case model.Account:
|
|
|
|
name = userData.(model.Account).Account
|
|
|
|
case twitch.UserInfo:
|
2018-09-20 17:14:08 +00:00
|
|
|
name = userData.(twitch.UserInfo).Login
|
2018-09-16 15:39:15 +00:00
|
|
|
default:
|
|
|
|
c.LoginFirst(nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if ltype, ok = loginType.(string); !ok {
|
|
|
|
c.LoginFirst(nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
loginUser := map[string]string{
|
|
|
|
"name": name,
|
|
|
|
"type": ltype,
|
|
|
|
}
|
|
|
|
session.Set("loginUser", loginUser)
|
|
|
|
session.Save()
|
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
|
2018-08-28 03:55:38 +00:00
|
|
|
// UserLogin - system user login
|
|
|
|
func UserLogin(c *context.Context) {
|
|
|
|
bodyArg := struct {
|
|
|
|
Account string `form:"account" json:"account" binding:"required"`
|
|
|
|
Password string `form:"password" json:"password" binding:"required"`
|
|
|
|
}{}
|
|
|
|
err := c.BindData(&bodyArg)
|
|
|
|
if err != nil {
|
|
|
|
c.DataFormat(nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
acc, err := model.GetAccount(bodyArg.Account)
|
|
|
|
if err != nil {
|
2018-09-02 01:15:27 +00:00
|
|
|
c.ServerError(`account or password error`)
|
2018-08-28 03:55:38 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = bcrypt.CompareHashAndPassword([]byte(acc.Password), []byte(bodyArg.Password))
|
|
|
|
if err != nil {
|
|
|
|
c.DataFormat(`account or password error`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-02 01:15:27 +00:00
|
|
|
session := sessions.Default(c.Context)
|
|
|
|
|
2018-09-16 15:39:15 +00:00
|
|
|
acc.Password = ""
|
|
|
|
session.Set("user", acc)
|
2018-09-02 01:15:27 +00:00
|
|
|
session.Set("loginType", "system")
|
|
|
|
session.Save()
|
|
|
|
|
|
|
|
c.Success(nil)
|
2018-08-28 03:55:38 +00:00
|
|
|
}
|
2018-09-07 08:33:37 +00:00
|
|
|
|
|
|
|
// UserLogout -
|
|
|
|
func UserLogout(c *context.Context) {
|
|
|
|
session := sessions.Default(c.Context)
|
|
|
|
session.Clear()
|
|
|
|
session.Save()
|
|
|
|
|
|
|
|
c.Success(nil)
|
|
|
|
}
|
2018-09-20 17:14:08 +00:00
|
|
|
|
|
|
|
// GetSessionData -
|
|
|
|
func GetSessionData(c *context.Context) {
|
|
|
|
session := sessions.Default(c.Context)
|
|
|
|
loginUser := session.Get("loginUser")
|
|
|
|
if loginUser == nil {
|
|
|
|
c.LoginFirst(nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user := map[string]interface{}{
|
|
|
|
"user": loginUser,
|
|
|
|
}
|
|
|
|
c.Success(user)
|
|
|
|
}
|