55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package controllers
|
|
|
|
import (
|
|
"cloud.o-forge.io/core/oc-catalog/models"
|
|
beego "github.com/beego/beego/v2/server/web"
|
|
)
|
|
|
|
type UserController struct {
|
|
beego.Controller
|
|
}
|
|
|
|
// @Title Login
|
|
// @Description Logs user into the system
|
|
// @Param username query string true "The username for login"
|
|
// @Param password query string true "The password for login"
|
|
// @Success 200 {string} login success
|
|
// @Failure 403 user not exist
|
|
// @router /login [get]
|
|
func (u *UserController) Login() {
|
|
username := u.GetString("username")
|
|
password := u.GetString("password")
|
|
|
|
if models.Login(username, password) {
|
|
token, err := CreateToken(username)
|
|
|
|
if err != nil {
|
|
u.Data["json"] = err.Error()
|
|
u.Ctx.Output.Status = 503
|
|
u.ServeJSON()
|
|
return
|
|
}
|
|
|
|
u.Ctx.SetCookie("token", token)
|
|
u.Ctx.Output.Header("Authorization", token) //FIXME: Some more generic way to use the name of the header
|
|
u.Data["json"] = "login success"
|
|
u.ServeJSON()
|
|
return
|
|
|
|
}
|
|
u.Ctx.Output.Status = 403
|
|
u.Data["json"] = "user not exist"
|
|
u.ServeJSON()
|
|
|
|
}
|
|
|
|
// @Title logout
|
|
// @Description Logs out current logged in user session
|
|
// @Success 200 {string} logout success
|
|
// // @Security mySecurityPathNameApiKey
|
|
// @router /logout [get]
|
|
func (u *UserController) Logout() {
|
|
u.Data["json"] = "logout success"
|
|
u.ServeJSON()
|
|
}
|