This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
73 lines (53 loc) · 2.02 KB
/
auth.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package web
import (
"fmt"
"net/http"
"github.com/eveisesi/skillz"
"github.com/eveisesi/skillz/internal/errors"
"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/buffalo/render"
)
func (s *Service) logoutHandler(c buffalo.Context) error {
c.Session().Clear()
c.Flash().Add("success", "You've been logged out successfully")
return c.Redirect(http.StatusFound, "rootPath()")
}
func (s *Service) loginGetHandler(c buffalo.Context) error {
c.Set("title", fmt.Sprintf("Welcome to Skillboard.Evie %s", titleSuffix))
var ctx = c.Request().Context()
code, state := c.Param("code"), c.Param("state")
if code != "" && state != "" {
user, err := s.user.Login(ctx, code, state)
if err != nil {
s.logger.WithError(err).Error("failed to query user from attempt")
return errors.NewBuffaloHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to query user from attempt"))
}
if user.IsNew {
s.logger.WithField("user", user.ID).Info("processing new user")
err = s.processor.ProcessUser(ctx, user)
if err != nil {
s.logger.WithError(err).WithField("user", user.ID).Error("failed to process user")
s.flashDanger(c, "Failed to fetch user data. Please try again. If error persists, join us on Discord")
return c.Render(http.StatusOK, s.renderer.HTML("login/index.plush.html"))
}
}
c.Session().Set(keyAuthenticatedUserID, user.ID)
return c.Redirect(http.StatusFound, "userPath()", render.Data{"userID": user.ID})
}
return c.Render(http.StatusOK, s.renderer.HTML("login/index.plush.html"))
}
func (s *Service) loginPostHandler(c buffalo.Context) error {
var r = c.Request()
var ctx = r.Context()
var form = r.Form
scopes := []string{skillz.ReadSkillsV1.String(), skillz.ReadSkillQueueV1.String()}
if form.Has("allow_implants") {
scopes = append(scopes, skillz.ReadImplantsV1.String())
}
attempt, err := s.auth.InitializeAttempt(ctx)
if err != nil {
return err
}
authorizationURL := s.auth.AuthorizationURI(ctx, attempt.State, scopes)
return c.Redirect(http.StatusFound, authorizationURL)
}