Skip to content

Commit ad4fde2

Browse files
committed
feat: Enhance OAuth login button configuration and rendering
1 parent 5a73975 commit ad4fde2

File tree

3 files changed

+58
-44
lines changed

3 files changed

+58
-44
lines changed

custom/OAuthLoginButton.vue

-26
This file was deleted.

custom/OAuthLoginButtons.vue

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<template>
2+
<div :class="meta.iconOnly ? 'flex flex-row justify-center items-center gap-3' : 'flex flex-col justify-center items-center gap-2'" >
3+
<button
4+
v-for="provider in meta.providers"
5+
:key="provider.provider"
6+
@click="handleLogin(provider.authUrl)"
7+
class="border dark:border-gray-400 flex items-center justify-center hover:bg-gray-50 hover:dark:border-gray-300 hover:dark:bg-gray-700"
8+
:class="[
9+
meta.iconOnly ? 'w-11 h-11 p-0' : 'w-full py-2 px-4',
10+
meta.pill ? 'rounded-full' : 'rounded-md'
11+
]"
12+
>
13+
<div v-html="provider.icon" class="w-6 h-6" :class="meta.iconOnly ? 'mr-0' : 'mr-4'" :alt="getProviderName(provider.provider)" />
14+
<span v-if="!meta.iconOnly" class="font-medium dark:text-white">Continue with {{ getProviderName(provider.provider) }}</span>
15+
</button>
16+
</div>
17+
</template>
18+
19+
<script setup>
20+
const props = defineProps({
21+
meta: {
22+
type: Object,
23+
required: true
24+
}
25+
});
26+
27+
const getProviderName = (provider) => {
28+
return provider.replace('AdminForthAdapter', '').replace('Oauth2', '');
29+
};
30+
31+
const handleLogin = (authUrl) => {
32+
window.location.href = authUrl;
33+
};
34+
</script>

index.ts

+24-18
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ interface OAuthPluginOptions {
1010
emailField: string;
1111
emailConfirmedField?: string;
1212
adapters: OAuth2Adapter[];
13+
iconOnly?: boolean;
14+
pill?: boolean;
15+
authenticationExpireDuration?: number;
1316
openSignup?: {
1417
enabled?: boolean;
1518
defaultFieldValues?: Record<string, any>;
@@ -30,6 +33,8 @@ export default class OAuthPlugin extends AdminForthPlugin {
3033
// Set default values for openSignup
3134
this.options = {
3235
...options,
36+
iconOnly: options.iconOnly ?? false,
37+
pill: options.pill ?? false,
3338
openSignup: {
3439
enabled: options.openSignup?.enabled ?? false,
3540
defaultFieldValues: options.openSignup?.defaultFieldValues ?? {},
@@ -43,11 +48,6 @@ export default class OAuthPlugin extends AdminForthPlugin {
4348
this.adminforth = adminforth;
4449
this.resource = resource;
4550

46-
// Add custom page for OAuth callback
47-
if (!adminforth.config.customization.customPages) {
48-
adminforth.config.customization.customPages = [];
49-
}
50-
5151
adminforth.config.customization.customPages.push({
5252
path: '/oauth/callback',
5353
component: {
@@ -84,24 +84,30 @@ export default class OAuthPlugin extends AdminForthPlugin {
8484
}
8585

8686
// Register the component with the correct plugin path
87-
const componentPath = `@@/plugins/${this.constructor.name}/OAuthLoginButton.vue`;
88-
this.componentPath('OAuthLoginButton.vue');
87+
const componentPath = `@@/plugins/${this.constructor.name}/OAuthLoginButtons.vue`;
88+
this.componentPath('OAuthLoginButtons.vue');
8989

9090
const baseUrl = adminforth.config.baseUrl || '';
91-
this.options.adapters.forEach(adapter => {
91+
const providers = this.options.adapters.map(adapter => {
9292
const state = Buffer.from(JSON.stringify({
9393
provider: adapter.constructor.name
9494
})).toString('base64');
9595

96-
adminforth.config.customization.loginPageInjections.underInputs.push({
97-
file: componentPath,
98-
meta: {
99-
authUrl: `${adapter.getAuthUrl()}&state=${state}`,
100-
provider: adapter.constructor.name,
101-
baseUrl,
102-
icon: adapter.getIcon()
103-
}
104-
});
96+
return {
97+
authUrl: `${adapter.getAuthUrl()}&state=${state}`,
98+
provider: adapter.constructor.name,
99+
baseUrl,
100+
icon: adapter.getIcon(),
101+
};
102+
});
103+
104+
adminforth.config.customization.loginPageInjections.underInputs.push({
105+
file: componentPath,
106+
meta: {
107+
providers,
108+
iconOnly: this.options.iconOnly,
109+
pill: this.options.pill,
110+
}
105111
});
106112
}
107113

@@ -136,7 +142,7 @@ export default class OAuthPlugin extends AdminForthPlugin {
136142
response,
137143
username,
138144
pk: user.id,
139-
expireInDays: this.adminforth.config.auth.rememberMeDays
145+
expireInDays: this.options.authenticationExpireDuration ? this.options.authenticationExpireDuration : this.adminforth.config.auth.rememberMeDays
140146
});
141147
}
142148

0 commit comments

Comments
 (0)