Link handles POST /api/v1/auth/discord/link.
(w http.ResponseWriter, r *http.Request)
| 340 | |
| 341 | // Link handles POST /api/v1/auth/discord/link. |
| 342 | func (h *DiscordHandler) Link(w http.ResponseWriter, r *http.Request) { |
| 343 | if h.discordStore == nil { |
| 344 | Error(w, http.StatusServiceUnavailable, "Discord authentication is not available") |
| 345 | return |
| 346 | } |
| 347 | userID, _ := r.Context().Value(middleware.UserIDKey).(string) |
| 348 | if userID == "" { |
| 349 | Error(w, http.StatusUnauthorized, "Unauthorized") |
| 350 | return |
| 351 | } |
| 352 | cfg, err := h.loadDiscordConfig(r.Context()) |
| 353 | if err != nil || cfg == nil { |
| 354 | Error(w, http.StatusBadRequest, "Discord authentication is not enabled") |
| 355 | return |
| 356 | } |
| 357 | state, err := discord.GenerateState() |
| 358 | if err != nil { |
| 359 | Error(w, http.StatusInternalServerError, "Failed to generate Discord link URL") |
| 360 | return |
| 361 | } |
| 362 | codeVerifier, _, err := discord.GeneratePKCE() |
| 363 | if err != nil { |
| 364 | Error(w, http.StatusInternalServerError, "Failed to generate Discord link URL") |
| 365 | return |
| 366 | } |
| 367 | authURL, err := cfg.GenerateAuthURL(state, codeVerifier) |
| 368 | if err != nil { |
| 369 | Error(w, http.StatusInternalServerError, "Failed to generate Discord link URL") |
| 370 | return |
| 371 | } |
| 372 | sessionData := &store.DiscordSessionData{ |
| 373 | CodeVerifier: codeVerifier, |
| 374 | State: state, |
| 375 | Mode: "link", |
| 376 | UserID: userID, |
| 377 | CreatedAt: time.Now().UnixMilli(), |
| 378 | } |
| 379 | if err := h.discordStore.Store(r.Context(), state, sessionData, 10*time.Minute); err != nil { |
| 380 | Error(w, http.StatusInternalServerError, "Failed to generate Discord link URL") |
| 381 | return |
| 382 | } |
| 383 | JSON(w, http.StatusOK, map[string]interface{}{"url": authURL}) |
| 384 | } |
| 385 | |
| 386 | // Unlink handles POST /api/v1/auth/discord/unlink. |
| 387 | func (h *DiscordHandler) Unlink(w http.ResponseWriter, r *http.Request) { |
nothing calls this directly
no test coverage detected