Skip to content

Commit 60b5c12

Browse files
committed
✨ Integrate GitHub authentication
1 parent b9119ca commit 60b5c12

9 files changed

Lines changed: 346 additions & 6 deletions

File tree

‎.env.example‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ DB_HOST=127.0.0.1
99
DB_PORT=3306
1010
DB_USER=root
1111
DB_PASSWORD=root
12-
DB_DATABASE=app
12+
DB_DATABASE=app
13+
GITHUB_CLIENT_ID=
14+
GITHUB_CLIENT_SECRET=
15+
GITHUB_CALLBACK_URL=

‎adonisrc.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export default defineConfig({
3838
() => import('@adonisjs/lucid/database_provider'),
3939
() => import('@adonisjs/auth/auth_provider'),
4040
() => import('@adonisjs/inertia/inertia_provider'),
41+
() => import('@adonisjs/ally/ally_provider'),
4142
],
4243

4344
/*
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { HttpContext } from '@adonisjs/core/http'
2+
import User from '#models/user'
3+
import type { GithubDriver } from '@adonisjs/ally/drivers/github'
4+
5+
export default class GithubController {
6+
async redirect({ ally }: HttpContext) {
7+
return (ally.use('github') as GithubDriver).redirect()
8+
}
9+
10+
async callback({ ally, auth, response }: HttpContext) {
11+
const github = ally.use('github') as GithubDriver
12+
13+
/**
14+
* User has explicitly cancelled the login flow
15+
*/
16+
if (github.accessDenied()) {
17+
return response.redirect().toRoute('login')
18+
}
19+
20+
/**
21+
* OAuth state verification failed. This happens when the
22+
* CSRF cookie gets expired or request is forged
23+
*/
24+
if (github.stateMisMatch()) {
25+
return response.redirect().toRoute('login')
26+
}
27+
28+
/**
29+
* GitHub responded with some error
30+
*/
31+
if (github.hasError()) {
32+
return response.redirect().toRoute('login')
33+
}
34+
35+
/**
36+
* Get user details from GitHub
37+
*/
38+
const githubUser = await github.user()
39+
40+
/**
41+
* Find or create user by GitHub ID
42+
*/
43+
const user = await User.firstOrCreate(
44+
{
45+
githubId: githubUser.id,
46+
},
47+
{
48+
username: githubUser.nickName,
49+
name: githubUser.name,
50+
email: githubUser.email!,
51+
avatar: githubUser.avatarUrl,
52+
githubId: githubUser.id,
53+
}
54+
)
55+
56+
/**
57+
* Login user using the web guard
58+
*/
59+
await auth.use('web').login(user)
60+
return response.redirect().toRoute('dashboard')
61+
}
62+
}

‎config/ally.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import env from '#start/env'
2+
import { defineConfig, services } from '@adonisjs/ally'
3+
4+
const allyConfig = defineConfig({
5+
github: services.github({
6+
clientId: env.get('GITHUB_CLIENT_ID'),
7+
clientSecret: env.get('GITHUB_CLIENT_SECRET'),
8+
callbackUrl: env.get('GITHUB_CALLBACK_URL'),
9+
}),
10+
})
11+
12+
export default allyConfig

‎inertia/pages/auth/login.tsx‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useForm } from '@inertiajs/react'
1+
import { Link, useForm } from '@inertiajs/react'
22
import { FormEvent } from 'react'
33
import { router } from '@inertiajs/react'
44

@@ -22,6 +22,31 @@ export default function Login() {
2222
Sign in to your account
2323
</h2>
2424
</div>
25+
{/* Social Login */}
26+
<div className="space-y-3">
27+
<Link
28+
href="/auth/github"
29+
className="w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-gray-800 hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-gray-500"
30+
>
31+
<svg className="w-5 h-5 mr-2" fill="currentColor" viewBox="0 0 20 20">
32+
<path
33+
fillRule="evenodd"
34+
d="M10 0C4.477 0 0 4.484 0 10.017c0 4.425 2.865 8.18 6.839 9.504.5.092.682-.217.682-.483 0-.237-.008-.868-.013-1.703-2.782.605-3.369-1.343-3.369-1.343-.454-1.158-1.11-1.466-1.11-1.466-.908-.62.069-.608.069-.608 1.003.07 1.531 1.032 1.531 1.032.892 1.53 2.341 1.088 2.91.832.092-.647.35-1.088.636-1.338-2.22-.253-4.555-1.113-4.555-4.951 0-1.093.39-1.988 1.029-2.688-.103-.253-.446-1.272.098-2.65 0 0 .84-.27 2.75 1.026A9.564 9.564 0 0110 4.844c.85.004 1.705.115 2.504.337 1.909-1.296 2.747-1.027 2.747-1.027.546 1.379.203 2.398.1 2.651.64.7 1.028 1.595 1.028 2.688 0 3.848-2.339 4.695-4.566 4.942.359.31.678.921.678 1.856 0 1.338-.012 2.419-.012 2.747 0 .268.18.58.688.482A10.019 10.019 0 0020 10.017C20 4.484 15.522 0 10 0z"
35+
clipRule="evenodd"
36+
/>
37+
</svg>
38+
Continue with GitHub
39+
</Link>
40+
</div>
41+
42+
<div className="relative">
43+
<div className="absolute inset-0 flex items-center">
44+
<div className="w-full border-t border-gray-300" />
45+
</div>
46+
<div className="relative flex justify-center text-sm">
47+
<span className="px-2 bg-gray-50 text-gray-500">Or continue with</span>
48+
</div>
49+
</div>
2550
<form className="mt-8 space-y-6" onSubmit={handleSubmit}>
2651
<div className="rounded-md shadow-sm -space-y-px">
2752
<div>

0 commit comments

Comments
 (0)