-
Notifications
You must be signed in to change notification settings - Fork 0
/
authentication.go
51 lines (42 loc) · 1.2 KB
/
authentication.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
package api
import (
"errors"
"log"
"net/http"
)
func authenticationRoutes() {
router.HandleFunc("/login", handlerWithSession(login)).Methods("POST")
router.HandleFunc("/logout", handlerWithSession(logout)).Methods("POST")
}
func login(req *http.Request, session *sessionData) (interface{}, error) {
log.Println("Call - login")
if !session.Auth {
var form loginForm
valid := retrieveForm(&form, req, loginFormValidation)
if valid.HasErrors() {
log.Println("Login form is invalid:", valid.Error())
return nil, errors.New("Invalid login form")
}
user, _ := getUserByEmail(form.Email)
if user == nil {
log.Println("Unable to find user:", form.Email)
return nil, errors.New("Unable to find user")
}
err := user.authenticate(form.Password)
if err != nil {
log.Println("Password check failed")
return nil, errors.New("Password check failed")
}
session.Auth = true
session.Email = user.Email
session.Name = user.Name
session.Roles = user.Roles
return nil, nil
}
return nil, errors.New("Already logged in")
}
func logout(req *http.Request, session *sessionData) (interface{}, error) {
log.Println("Call - logout")
session.MarkedForDeletion = true
return nil, nil
}