This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.go
55 lines (51 loc) · 1.49 KB
/
login.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package login
import (
"github.com/shoriwe/CAPitan/internal/web/http405"
"github.com/shoriwe/CAPitan/internal/web/middleware"
"github.com/shoriwe/CAPitan/internal/web/symbols"
"net/http"
)
func loginForm(mw *middleware.Middleware, context *middleware.Context) bool {
form, _ := mw.Templates.ReadFile("templates/login/login.html")
context.StatusCode = http.StatusOK
context.Body = string(form)
return false
}
func loginUser(mw *middleware.Middleware, context *middleware.Context) bool {
username := context.Request.PostFormValue(symbols.Username)
password := context.Request.PostFormValue(symbols.Password)
if username == "" || password == "" {
context.Redirect = symbols.Login
return false
}
user, succeed := mw.Login(context.Request, username, password)
if !succeed {
context.Redirect = symbols.Login
return false
}
var cookie string
cookie, succeed = mw.GenerateCookieFor(context.Request, user.Username, symbols.LoginSessionDuration)
if !succeed {
context.Redirect = symbols.Login
return false
}
context.Redirect = symbols.Dashboard
context.NewCookie = &http.Cookie{
Name: symbols.CookieName,
Value: cookie,
}
return false
}
func Login(mw *middleware.Middleware, context *middleware.Context) bool {
if context.User != nil {
context.Redirect = symbols.Dashboard
return false
}
switch context.Request.Method {
case http.MethodGet:
return loginForm(mw, context)
case http.MethodPost:
return loginUser(mw, context)
}
return http405.MethodNotAllowed(mw, context)
}