mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-23 18:54:03 +08:00
Merge two login functions.
This commit is contained in:
parent
d0e243fca3
commit
18c021b009
@ -17,12 +17,12 @@ package controllers
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/casdoor/casdoor/object"
|
||||
"github.com/casdoor/casdoor/util"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type RegisterForm struct {
|
||||
@ -97,43 +97,6 @@ func (c *ApiController) Register() {
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// @Title Login
|
||||
// @Description login as a user
|
||||
// @Param username formData string true "The username to login"
|
||||
// @Param password formData string true "The password"
|
||||
// @Success 200 {object} controllers.api_controller.Response The Response object
|
||||
// @router /login [post]
|
||||
func (c *ApiController) Login() {
|
||||
var resp Response
|
||||
|
||||
if c.GetSessionUser() != "" {
|
||||
resp = Response{Status: "error", Msg: "please log out first before signing in", Data: c.GetSessionUser()}
|
||||
c.Data["json"] = resp
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
var form RegisterForm
|
||||
err := json.Unmarshal(c.Ctx.Input.RequestBody, &form)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
userId := fmt.Sprintf("%s/%s", form.Organization, form.Username)
|
||||
password := form.Password
|
||||
msg := object.CheckUserLogin(userId, password)
|
||||
|
||||
if msg != "" {
|
||||
resp = Response{Status: "error", Msg: msg, Data: ""}
|
||||
} else {
|
||||
c.HandleLoggedIn(userId)
|
||||
resp = Response{Status: "ok", Msg: "", Data: userId}
|
||||
}
|
||||
|
||||
c.Data["json"] = resp
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// @Title Logout
|
||||
// @Description logout the current user
|
||||
// @Success 200 {object} controllers.api_controller.Response The Response object
|
||||
|
@ -31,13 +31,33 @@ func (c *ApiController) HandleLoggedIn(userId string) {
|
||||
util.LogInfo(c.Ctx, "API: [%s] signed in", userId)
|
||||
}
|
||||
|
||||
func (c *ApiController) AuthLogin() {
|
||||
func (c *ApiController) Login() {
|
||||
var resp Response
|
||||
var form RegisterForm
|
||||
err := json.Unmarshal(c.Ctx.Input.RequestBody, &form)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if form.Username != "" {
|
||||
if c.GetSessionUser() != "" {
|
||||
resp = Response{Status: "error", Msg: "please log out first before signing in", Data: c.GetSessionUser()}
|
||||
c.Data["json"] = resp
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
userId := fmt.Sprintf("%s/%s", form.Organization, form.Username)
|
||||
password := form.Password
|
||||
msg := object.CheckUserLogin(userId, password)
|
||||
|
||||
if msg != "" {
|
||||
resp = Response{Status: "error", Msg: msg, Data: ""}
|
||||
} else {
|
||||
c.HandleLoggedIn(userId)
|
||||
resp = Response{Status: "ok", Msg: "", Data: userId}
|
||||
}
|
||||
} else if form.Provider != "" {
|
||||
application := object.GetApplication(fmt.Sprintf("admin/%s", form.Application))
|
||||
provider := object.GetProvider(fmt.Sprintf("admin/%s", form.Provider))
|
||||
|
||||
@ -47,7 +67,6 @@ func (c *ApiController) AuthLogin() {
|
||||
oauthConfig.ClientSecret = provider.ClientSecret
|
||||
oauthConfig.RedirectURL = form.RedirectUri
|
||||
|
||||
var resp Response
|
||||
var res authResponse
|
||||
|
||||
if form.State != beego.AppConfig.String("AuthState") {
|
||||
@ -114,7 +133,6 @@ func (c *ApiController) AuthLogin() {
|
||||
}
|
||||
}
|
||||
}
|
||||
//res.Method = res.Email
|
||||
resp = Response{Status: "ok", Msg: "success", Data: res}
|
||||
} else {
|
||||
userId := c.GetSessionUser()
|
||||
@ -125,11 +143,11 @@ func (c *ApiController) AuthLogin() {
|
||||
return
|
||||
}
|
||||
|
||||
var linkRes bool
|
||||
linkRes := false
|
||||
if provider.Type == "github" {
|
||||
_ = object.LinkUserAccount(userId, "github", res.Method)
|
||||
linkRes = object.LinkUserAccount(userId, "github", res.Method)
|
||||
} else if provider.Type == "google" {
|
||||
_ = object.LinkUserAccount(userId, "google", res.Email)
|
||||
linkRes = object.LinkUserAccount(userId, "google", res.Email)
|
||||
}
|
||||
if linkRes {
|
||||
resp = Response{Status: "ok", Msg: "success", Data: linkRes}
|
||||
@ -141,6 +159,9 @@ func (c *ApiController) AuthLogin() {
|
||||
// object.LinkUserAccount(userId, "avatar", avatar)
|
||||
//}
|
||||
}
|
||||
} else {
|
||||
panic("unknown authentication type (not password or provider)")
|
||||
}
|
||||
|
||||
c.Data["json"] = resp
|
||||
c.ServeJSON()
|
||||
|
@ -37,7 +37,6 @@ func initAPI() {
|
||||
beego.Router("/api/login", &controllers.ApiController{}, "POST:Login")
|
||||
beego.Router("/api/logout", &controllers.ApiController{}, "POST:Logout")
|
||||
beego.Router("/api/get-account", &controllers.ApiController{}, "GET:GetAccount")
|
||||
beego.Router("/api/auth/login", &controllers.ApiController{}, "POST:AuthLogin")
|
||||
|
||||
beego.Router("/api/get-organizations", &controllers.ApiController{}, "GET:GetOrganizations")
|
||||
beego.Router("/api/get-organization", &controllers.ApiController{}, "GET:GetOrganization")
|
||||
|
@ -44,14 +44,6 @@ export function logout() {
|
||||
}).then(res => res.json());
|
||||
}
|
||||
|
||||
export function authLogin(values) {
|
||||
return fetch(`${authConfig.serverUrl}/api/auth/login`, {
|
||||
method: 'POST',
|
||||
credentials: "include",
|
||||
body: JSON.stringify(values),
|
||||
}).then(res => res.json());
|
||||
}
|
||||
|
||||
export function getOAuthCode(clientId, responseType, redirectUri, scope, state) {
|
||||
return fetch(`${authConfig.serverUrl}/api/oauth/code?clientId=${clientId}&responseType=${responseType}&redirectUri=${redirectUri}&scope=${scope}&state=${state}`, {
|
||||
method: 'GET',
|
||||
|
@ -46,7 +46,7 @@ class AuthCallback extends React.Component {
|
||||
redirectUri: redirectUri,
|
||||
method: this.state.method,
|
||||
};
|
||||
AuthBackend.authLogin(body)
|
||||
AuthBackend.login(body)
|
||||
.then((res) => {
|
||||
if (res.status === "ok") {
|
||||
window.location.href = '/';
|
||||
|
@ -3380,20 +3380,13 @@ copy-descriptor@^0.1.0:
|
||||
resolved "https://registry.npm.taobao.org/copy-descriptor/download/copy-descriptor-0.1.1.tgz#676f6eb3c39997c2ee1ac3a924fd6124748f578d"
|
||||
integrity sha1-Z29us8OZl8LuGsOpJP1hJHSPV40=
|
||||
|
||||
copy-to-clipboard@^3.2.0:
|
||||
copy-to-clipboard@^3.2.0, copy-to-clipboard@^3.3.1:
|
||||
version "3.3.1"
|
||||
resolved "https://registry.npm.taobao.org/copy-to-clipboard/download/copy-to-clipboard-3.3.1.tgz#115aa1a9998ffab6196f93076ad6da3b913662ae"
|
||||
integrity sha1-EVqhqZmP+rYZb5MHatbaO5E2Yq4=
|
||||
dependencies:
|
||||
toggle-selection "^1.0.6"
|
||||
|
||||
copy-to-clipboard@^3.3.1:
|
||||
version "3.3.1"
|
||||
resolved "https://registry.npmjs.org/copy-to-clipboard/-/copy-to-clipboard-3.3.1.tgz#115aa1a9998ffab6196f93076ad6da3b913662ae"
|
||||
integrity sha512-i13qo6kIHTTpCm8/Wup+0b1mVWETvu2kIMzKoK8FpkLkFxlt0znUAHcMzox+T8sPlqtZXq3CulEjQHsYiGFJUw==
|
||||
dependencies:
|
||||
toggle-selection "^1.0.6"
|
||||
|
||||
core-js-compat@^3.6.2:
|
||||
version "3.6.5"
|
||||
resolved "https://registry.npm.taobao.org/core-js-compat/download/core-js-compat-3.6.5.tgz#2a51d9a4e25dfd6e690251aa81f99e3c05481f1c"
|
||||
|
Loading…
x
Reference in New Issue
Block a user