Skip to content

Commit 9254cf3

Browse files
committed
feat: Add OAuth callback page for handling authentication flow
1 parent 5ca5fd2 commit 9254cf3

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

custom/OAuthCallback.vue

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<template>
2+
<div class="flex items-center justify-center min-h-screen">
3+
<Spinner />
4+
</div>
5+
</template>
6+
7+
<script setup>
8+
import { onMounted } from 'vue';
9+
import { useUserStore } from '@/stores/user';
10+
import { useRouter } from 'vue-router';
11+
import { callAdminForthApi } from '@/utils';
12+
import { Spinner } from '@/afcl';
13+
14+
const router = useRouter();
15+
const userStore = useUserStore();
16+
17+
onMounted(async () => {
18+
const urlParams = new URLSearchParams(window.location.search);
19+
const code = urlParams.get('code');
20+
const state = urlParams.get('state');
21+
22+
if (code && state) {
23+
const encodedCode = encodeURIComponent(code);
24+
const encodedState = encodeURIComponent(state);
25+
const response = await callAdminForthApi({
26+
path: `/oauth/callback?code=${encodedCode}&state=${encodedState}`,
27+
method: 'GET',
28+
});
29+
if (response.allowedLogin) {
30+
await userStore.finishLogin();
31+
} else if (response.redirectTo) {
32+
router.push(response.redirectTo);
33+
} else if (response.error) {
34+
router.push({
35+
name: 'login',
36+
query: { error: response.error }
37+
});
38+
}
39+
} else {
40+
router.push({ name: 'login' });
41+
}
42+
});
43+
</script>

index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ export class OAuthPlugin extends AdminForthPlugin {
3131
this.adminforth = adminforth;
3232
this.resource = resource;
3333

34+
// Add custom page for OAuth callback
35+
if (!adminforth.config.customization.customPages) {
36+
adminforth.config.customization.customPages = [];
37+
}
38+
39+
adminforth.config.customization.customPages.push({
40+
path: '/oauth/callback',
41+
component: {
42+
file: this.componentPath('OAuthCallback.vue'),
43+
meta: {
44+
title: 'OAuth Callback',
45+
customLayout: true
46+
}
47+
}
48+
});
49+
3450
// Validate emailField exists in resource
3551
if (!resource.columns.find(col => col.name === this.options.emailField)) {
3652
throw new Error(`OAuthPlugin: emailField "${this.options.emailField}" not found in resource columns`);

0 commit comments

Comments
 (0)