modweb/examples/test/discord-login/main.go

73 lines
1.3 KiB
Go

package discord_login
import (
"git.ddd.rip/ptrcnull/modweb"
"github.com/gofiber/fiber/v2"
"log"
)
type Module struct {}
const API_ENDPOINT = "https://discord.com/api/v6"
func (m *Module) LoginURL() string {
return "https://discord.com/api/oauth2/authorize?client_id=560595816289533954&redirect_uri=http%3A%2F%2Flocalhost%3A8011%2Fdiscord%2Fcallback&response_type=code&scope=identify%20email"
}
func (m *Module) RegisterURL() string {
return ""
}
func (m *Module) FriendlyName() string {
return "Discord"
}
func (m *Module) Name() string {
return "discord"
}
func (m *Module) MinAccessLevel() int64 {
return -1
}
func (m *Module) Hidden() bool {
return true
}
func (m *Module) Init(mm *modweb.ModuleManager) {
app := mm.Fiber()
app.Get("/callback", func(ctx *fiber.Ctx) error {
session := mm.Session(ctx)
defer session.Save()
if session.Get("user") != nil {
return ctx.Redirect("/")
}
code := ctx.Query("code")
if code == "" {
return ctx.Redirect("/")
}
token, err := getToken(code)
if err != nil {
log.Println(err)
return ctx.Redirect("/")
}
me, err := getMe(token)
if err != nil {
log.Println(err)
return ctx.Redirect("/")
}
user := &modweb.User{
ID: "discord:" + me.ID,
DisplayName: me.Username,
AccessLevel: 2137,
}
user.Save(session)
return ctx.Redirect("/")
})
}