MCPcopy Create free account
hub / github.com/PatchMon/PatchMon / Login

Method Login

server-source-code/internal/handler/discord.go:106–165  ·  view source on GitHub ↗

Login handles GET /api/v1/auth/discord/login.

(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

104
105// Login handles GET /api/v1/auth/discord/login.
106func (h *DiscordHandler) Login(w http.ResponseWriter, r *http.Request) {
107 if h.discordStore == nil {
108 Error(w, http.StatusServiceUnavailable, "Discord authentication is not available")
109 return
110 }
111 cfg, err := h.loadDiscordConfig(r.Context())
112 if err != nil || cfg == nil {
113 Error(w, http.StatusBadRequest, "Discord is not fully configured. Please set Client ID and Client Secret in Settings > Discord Auth.")
114 return
115 }
116 state, err := discord.GenerateState()
117 if err != nil {
118 if h.log != nil {
119 h.log.Error("discord generate state failed", "error", err)
120 }
121 Error(w, http.StatusInternalServerError, "Failed to initiate Discord login")
122 return
123 }
124 codeVerifier, _, err := discord.GeneratePKCE()
125 if err != nil {
126 if h.log != nil {
127 h.log.Error("discord generate pkce failed", "error", err)
128 }
129 Error(w, http.StatusInternalServerError, "Failed to initiate Discord login")
130 return
131 }
132 authURL, err := cfg.GenerateAuthURL(state, codeVerifier)
133 if err != nil {
134 if h.log != nil {
135 h.log.Error("discord auth url failed", "error", err)
136 }
137 Error(w, http.StatusInternalServerError, "Failed to initiate Discord login")
138 return
139 }
140 sessionData := &store.DiscordSessionData{
141 CodeVerifier: codeVerifier,
142 State: state,
143 Mode: "login",
144 CreatedAt: time.Now().UnixMilli(),
145 }
146 ttl := 10 * time.Minute
147 if err := h.discordStore.Store(r.Context(), state, sessionData, ttl); err != nil {
148 if h.log != nil {
149 h.log.Error("discord store session failed", "error", err)
150 }
151 Error(w, http.StatusInternalServerError, "Failed to initiate Discord login")
152 return
153 }
154 secure := h.cfg.Env == "production" && (r.TLS != nil || r.Header.Get("X-Forwarded-Proto") == "https")
155 http.SetCookie(w, &http.Cookie{
156 Name: "discord_state",
157 Value: state,
158 Path: "/",
159 MaxAge: 600,
160 HttpOnly: true,
161 Secure: secure,
162 SameSite: http.SameSiteLaxMode,
163 })

Callers

nothing calls this directly

Calls 6

loadDiscordConfigMethod · 0.95
ErrorFunction · 0.85
ErrorMethod · 0.80
GenerateAuthURLMethod · 0.80
StoreMethod · 0.45
GetMethod · 0.45

Tested by

no test coverage detected