diff options
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/controllers/controllers.go | 63 | ||||
| -rw-r--r-- | backend/controllers/funcs.go | 16 | ||||
| -rw-r--r-- | backend/routes/routes.go | 16 |
3 files changed, 95 insertions, 0 deletions
diff --git a/backend/controllers/controllers.go b/backend/controllers/controllers.go new file mode 100644 index 0000000..a24fc3c --- /dev/null +++ b/backend/controllers/controllers.go | |||
| @@ -0,0 +1,63 @@ | |||
| 1 | package controllers | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "log" | ||
| 5 | "net/http" | ||
| 6 | |||
| 7 | "github.com/gin-contrib/sessions" | ||
| 8 | "github.com/gin-gonic/gin" | ||
| 9 | "github.com/solovev/steam_go" | ||
| 10 | ) | ||
| 11 | |||
| 12 | func Home(c *gin.Context) { | ||
| 13 | session := sessions.Default(c) | ||
| 14 | if session.Get("id") == nil { | ||
| 15 | c.JSON(200, "no id, not auth") | ||
| 16 | } else { | ||
| 17 | var user *steam_go.PlayerSummaries | ||
| 18 | user, err := steam_go.GetPlayerSummaries(session.Get("id").(string), GetEnvKey("API_KEY")) | ||
| 19 | if err != nil { | ||
| 20 | c.JSON(200, "authenticated, but err") | ||
| 21 | log.Panic(err) | ||
| 22 | } else { | ||
| 23 | c.JSON(200, gin.H{ | ||
| 24 | "output": user, | ||
| 25 | }) | ||
| 26 | } | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | func Login(c *gin.Context) { | ||
| 31 | opId := steam_go.NewOpenId(c.Request) | ||
| 32 | switch opId.Mode() { | ||
| 33 | case "": | ||
| 34 | http.Redirect(c.Writer, c.Request, opId.AuthUrl(), 301) | ||
| 35 | case "cancel": | ||
| 36 | c.Writer.Write([]byte("Authorization cancelled")) | ||
| 37 | default: | ||
| 38 | steamId, err := opId.ValidateAndGetId() | ||
| 39 | if err != nil { | ||
| 40 | http.Error(c.Writer, err.Error(), http.StatusInternalServerError) | ||
| 41 | } | ||
| 42 | session := sessions.Default(c) | ||
| 43 | session.Set("id", steamId) | ||
| 44 | session.Save() | ||
| 45 | // Do whatever you want with steam id | ||
| 46 | c.Redirect(http.StatusMovedPermanently, "/") | ||
| 47 | c.Writer.Write([]byte(steamId)) | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | func Logout(c *gin.Context) { | ||
| 52 | session := sessions.Default(c) | ||
| 53 | if session.Get("id") == nil { | ||
| 54 | c.JSON(http.StatusBadRequest, "no id, not auth") | ||
| 55 | } else { | ||
| 56 | session.Set("id", "") | ||
| 57 | session.Clear() | ||
| 58 | session.Options(sessions.Options{Path: "/", MaxAge: -1}) | ||
| 59 | session.Save() | ||
| 60 | log.Print("id", session.Get("id")) | ||
| 61 | c.Redirect(http.StatusPermanentRedirect, "/") | ||
| 62 | } | ||
| 63 | } | ||
diff --git a/backend/controllers/funcs.go b/backend/controllers/funcs.go new file mode 100644 index 0000000..6d0f854 --- /dev/null +++ b/backend/controllers/funcs.go | |||
| @@ -0,0 +1,16 @@ | |||
| 1 | package controllers | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "log" | ||
| 5 | "os" | ||
| 6 | |||
| 7 | "github.com/joho/godotenv" | ||
| 8 | ) | ||
| 9 | |||
| 10 | func GetEnvKey(key string) string { | ||
| 11 | err := godotenv.Load() | ||
| 12 | if err != nil { | ||
| 13 | log.Fatal("Error loading .env file") | ||
| 14 | } | ||
| 15 | return os.Getenv(key) | ||
| 16 | } | ||
diff --git a/backend/routes/routes.go b/backend/routes/routes.go new file mode 100644 index 0000000..5607252 --- /dev/null +++ b/backend/routes/routes.go | |||
| @@ -0,0 +1,16 @@ | |||
| 1 | package routes | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "github.com/gin-contrib/sessions" | ||
| 5 | "github.com/gin-contrib/sessions/cookie" | ||
| 6 | "github.com/gin-gonic/gin" | ||
| 7 | "github.com/pektezol/leastportals/backend/controllers" | ||
| 8 | ) | ||
| 9 | |||
| 10 | func InitRoutes(router *gin.Engine) { | ||
| 11 | store := cookie.NewStore([]byte(controllers.GetEnvKey("SESSION_KEY"))) | ||
| 12 | router.Use(sessions.Sessions("session", store)) | ||
| 13 | router.GET("/", controllers.Home) | ||
| 14 | router.GET("/login", controllers.Login) | ||
| 15 | router.GET("/logout", controllers.Logout) | ||
| 16 | } | ||