|
1 | | -import { SESSION_VERSION_KEY } from '#middleware/silent_auth_middleware' |
2 | | -import { DateTime } from 'luxon' |
3 | | -import string from '@adonisjs/core/helpers/string' |
4 | 1 | import type { HttpContext } from '@adonisjs/core/http' |
5 | | -import User from '#models/user' |
6 | | -import GithubOAuth from '#services/github_oauth' |
7 | | -import { githubEnabled } from '#config/ally' |
8 | | -import { RESERVED_USERNAMES } from '#validators/register_validator' |
| 2 | +import SocialController from '#controllers/auth/social_controller' |
9 | 3 |
|
| 4 | +/** |
| 5 | + * @deprecated Kept for backward compatibility only: the routes point to |
| 6 | + * SocialController ("/auth/:provider", "/auth/:provider/callback"), which |
| 7 | + * handles GitHub, Google and Apple with the same account rules. |
| 8 | + */ |
10 | 9 | export default class GithubController { |
11 | 10 | async redirect(ctx: HttpContext) { |
12 | | - if (!githubEnabled()) { |
13 | | - ctx.session.flash('error', 'La connexion avec GitHub n’est pas encore activée.') |
14 | | - return ctx.response.redirect().toRoute('login') |
15 | | - } |
16 | | - return ctx.response.redirect(new GithubOAuth(ctx).redirectUrl()) |
| 11 | + ctx.params.provider = 'github' |
| 12 | + return new SocialController().redirect(ctx) |
17 | 13 | } |
18 | 14 |
|
19 | 15 | async callback(ctx: HttpContext) { |
20 | | - const { auth, response, session } = ctx |
21 | | - if (!githubEnabled()) return response.redirect().toRoute('login') |
22 | | - |
23 | | - const profile = await new GithubOAuth(ctx).user().catch(() => null) |
24 | | - if (!profile) { |
25 | | - session.flash('error', 'Connexion GitHub annulée ou expirée. Réessayez.') |
26 | | - return response.redirect().toRoute('login') |
27 | | - } |
28 | | - |
29 | | - let user = await User.findBy('githubId', profile.id) |
30 | | - |
31 | | - if (!user && profile.email) { |
32 | | - // Link the GitHub account to an existing account with the same e-mail. |
33 | | - const existing = await User.findBy('email', profile.email.toLowerCase()) |
34 | | - if (existing && !existing.emailVerifiedAt) { |
35 | | - // Local e-mails are not verified: auto-linking would let anyone who |
36 | | - // registered with someone else's address capture their GitHub login. |
37 | | - session.flash( |
38 | | - 'error', |
39 | | - 'Un compte existe déjà avec cette adresse e-mail. Connectez-vous avec votre mot de passe.' |
40 | | - ) |
41 | | - return response.redirect().toRoute('login') |
42 | | - } |
43 | | - if (existing) { |
44 | | - existing.githubId = profile.id |
45 | | - existing.githubUsername = profile.login |
46 | | - existing.avatarUrl = existing.avatarUrl ?? profile.avatarUrl |
47 | | - await existing.save() |
48 | | - user = existing |
49 | | - } |
50 | | - } |
51 | | - |
52 | | - if (!user) { |
53 | | - if (!profile.email) { |
54 | | - session.flash( |
55 | | - 'error', |
56 | | - 'Votre compte GitHub n’expose aucune adresse e-mail vérifiée. Créez un compte avec votre e-mail.' |
57 | | - ) |
58 | | - return response.redirect().toRoute('register') |
59 | | - } |
60 | | - user = await User.create({ |
61 | | - githubId: profile.id, |
62 | | - githubUsername: profile.login, |
63 | | - username: await this.availableUsername(profile.login), |
64 | | - name: profile.name, |
65 | | - email: profile.email.toLowerCase(), |
66 | | - avatarUrl: profile.avatarUrl, |
67 | | - bio: profile.bio?.slice(0, 280) ?? null, |
68 | | - location: profile.location?.slice(0, 100) ?? null, |
69 | | - websiteUrl: |
70 | | - profile.blog && /^https?:\/\//.test(profile.blog) ? profile.blog.slice(0, 255) : null, |
71 | | - twitterUsername: profile.twitterUsername, |
72 | | - password: null, |
73 | | - role: 'member', |
74 | | - // GitHub only returns verified addresses. |
75 | | - emailVerifiedAt: DateTime.now(), |
76 | | - }) |
77 | | - session.flash('success', `Bienvenue dans la communauté, ${user.displayName} !`) |
78 | | - } else { |
79 | | - session.flash('success', `Bon retour, ${user.displayName} !`) |
80 | | - } |
81 | | - |
82 | | - if (user.isBanned) { |
83 | | - session.flash('error', 'Ce compte a été suspendu.') |
84 | | - return response.redirect().toRoute('login') |
85 | | - } |
86 | | - |
87 | | - await auth.use('web').login(user, true) |
88 | | - session.put(SESSION_VERSION_KEY, user.sessionVersion) |
89 | | - return response.redirect().toPath('/dashboard') |
90 | | - } |
91 | | - |
92 | | - private async availableUsername(login: string) { |
93 | | - const base = |
94 | | - login |
95 | | - .toLowerCase() |
96 | | - .replace(/[^a-z0-9_-]/g, '') |
97 | | - .slice(0, 24) || 'membre' |
98 | | - let candidate = RESERVED_USERNAMES.includes(base) ? `${base}-dev` : base |
99 | | - while (await User.findBy('username', candidate)) { |
100 | | - candidate = `${base}-${string.random(4).toLowerCase()}` |
101 | | - } |
102 | | - return candidate |
| 16 | + ctx.params.provider = 'github' |
| 17 | + return new SocialController().callback(ctx) |
103 | 18 | } |
104 | 19 | } |
0 commit comments