Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add GitHub oAuth2 login #124

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ AUTH_SECRET=generate-long-random-string-for-this
YOUTUBE_API_KEY=insert-your-api-key-here
GOOGLE_CLIENT_ID=insert-your-client-id-here
GOOGLE_CLIENT_SECRET=insert-your-client-secret-here
GITHUB_CLIENT_ID=insert-your-client-id-here
GITHUB_CLIENT_SECRET=insert-your-client-secret-here
REDIS_PORT=6379
REDIS_URL=redis://localhost:${REDIS_PORT}
DB_HOST=localhost
Expand Down
7 changes: 7 additions & 0 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { initAcceptLanguageHeaderDetector } from 'typesafe-i18n/detectors';
import { detectLocale } from '$lib/i18n/i18n-util.js';
import { SvelteKitAuth } from '@auth/sveltekit';
import Google from '@auth/core/providers/google';
import GitHub from '@auth/core/providers/github';
import PrismaAdapter from '$lib/prisma/client';
import { config } from '$/lib/config.server';
import prismaClient from '$/lib/db.server';
Expand All @@ -29,6 +30,12 @@ const handleAuth = (async (...args) => {
clientId: config.GOOGLE_CLIENT_ID,
clientSecret: config.GOOGLE_CLIENT_SECRET,
}),
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
GitHub({
clientId: config.GITHUB_CLIENT_ID,
clientSecret: config.GITHUB_CLIENT_SECRET,
}),
],
callbacks: {
async session({ session, user }) {
Expand Down
2 changes: 2 additions & 0 deletions src/lib/config.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export const ServerConfigSchema = z.object({
AUTH_SECRET: z.string().trim().min(32),
GOOGLE_CLIENT_ID: z.string().trim().min(1),
GOOGLE_CLIENT_SECRET: z.string().trim().min(1),
GITHUB_CLIENT_ID: z.string().trim().min(1),
GITHUB_CLIENT_SECRET: z.string().trim().min(1),
DB_HOST: z.string().trim().min(1),
DB_USER: z.string().trim().min(1),
DB_PASSWORD: z.string().trim().min(1),
Expand Down
1 change: 1 addition & 0 deletions src/lib/i18n/en/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const en: BaseTranslation = {
update: 'Update',
logOut: 'Logout',
loginYouTube: 'Login with YouTube',
loginGitHub: 'Login with GitHub',
},
labels: {
title: 'Title',
Expand Down
8 changes: 8 additions & 0 deletions src/lib/i18n/i18n-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ type RootTranslation = {
* L​o​g​i​n​ ​w​i​t​h​ ​Y​o​u​T​u​b​e
*/
loginYouTube: string
/**
* L​o​g​i​n​ ​w​i​t​h​ ​G​i​t​H​u​b
*/
loginGitHub: string
}
labels: {
/**
Expand Down Expand Up @@ -178,6 +182,10 @@ export type TranslationFunctions = {
* Login with YouTube
*/
loginYouTube: () => LocalizedString
/**
* Login with GitHub
*/
loginGitHub: () => LocalizedString
}
labels: {
/**
Expand Down
15 changes: 15 additions & 0 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,27 @@
signIn('google');
}}
disabled={loading}
data-testid="google-btn"
class="btn variant-filled-primary cursor-pointer">
{#if loading}
{$LL.messages.pleaseWait()} <ProgressRadial class="ml-2 h-6 w-6" stroke={100} />
{:else}
{$LL.buttons.loginYouTube()}
{/if}
</button>
<button
on:click|once={function loginClick() {
loading = true;
signIn('github');
}}
disabled={loading}
data-testid="github-btn"
class="btn variant-filled-primary cursor-pointer">
{#if loading}
{$LL.messages.pleaseWait()} <ProgressRadial class="ml-2 h-6 w-6" stroke={100} />
{:else}
{$LL.buttons.loginGitHub()}
{/if}
</button>
{/if}
</div>
8 changes: 7 additions & 1 deletion tests/oauth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { expect, test } from '@playwright/test';

test('login button redirects to google', async ({ page }) => {
await page.goto('/');
await page.getByRole('button').click();
await page.getByTestId('google-btn').click();
await page.waitForURL('**/accounts.google.com/**');
expect(page.url()).toContain('https://accounts.google.com/');
});
test('login button redirects to github', async ({ page }) => {
await page.goto('/');
await page.getByTestId('github-btn').click();
await page.waitForURL('**/github.com/**');
expect(page.url()).toContain('https://github.com/login');
});