-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
51 lines (40 loc) · 1.15 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
package server
import (
"fmt"
"net/http"
"github.com/jtarchie/forum/gothic"
"github.com/labstack/echo/v4"
)
func setupAuth(e *echo.Echo) {
e.GET("/auth/:provider/login", func(c echo.Context) error {
// try to get the user without re-authenticating
user, err := gothic.CompleteUserAuth(c)
if err == nil {
err = SessionPut("email", user.Email, c)
if err != nil {
return fmt.Errorf("could not persist user: %w", err)
}
return c.Redirect(http.StatusTemporaryRedirect, "/")
}
return gothic.BeginAuthHandler(c)
})
e.GET("/auth/:provider/callback", func(c echo.Context) error {
user, err := gothic.CompleteUserAuth(c)
if err != nil {
return fmt.Errorf("could not complete auth: %w", err)
}
err = SessionPut("email", user.Email, c)
if err != nil {
return fmt.Errorf("could not persist user: %w", err)
}
return c.Redirect(http.StatusTemporaryRedirect, "/")
})
e.GET("/auth/:provider/logout", func(c echo.Context) error {
_ = gothic.Logout(c)
err := SessionPut("email", "", c)
if err != nil {
return fmt.Errorf("could not clear user: %w", err)
}
return c.Redirect(http.StatusTemporaryRedirect, "/")
})
}