@witchcraft/nuxt-auth 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +693 -0
- package/dist/module.d.mts +170 -0
- package/dist/module.json +9 -0
- package/dist/module.mjs +146 -0
- package/dist/runtime/build/genAuthSecretKeys.d.ts +6 -0
- package/dist/runtime/build/genAuthSecretKeys.js +10 -0
- package/dist/runtime/components/ExternalCallback.d.vue.ts +7 -0
- package/dist/runtime/components/ExternalCallback.vue +96 -0
- package/dist/runtime/components/ExternalCallback.vue.d.ts +7 -0
- package/dist/runtime/components/ExternalCode.d.vue.ts +11 -0
- package/dist/runtime/components/ExternalCode.vue +77 -0
- package/dist/runtime/components/ExternalCode.vue.d.ts +11 -0
- package/dist/runtime/components/LoginProviderButtons.client.d.vue.ts +15 -0
- package/dist/runtime/components/LoginProviderButtons.client.vue +66 -0
- package/dist/runtime/components/LoginProviderButtons.client.vue.d.ts +15 -0
- package/dist/runtime/components/Mocker.d.vue.ts +2 -0
- package/dist/runtime/components/Mocker.vue +39 -0
- package/dist/runtime/components/Mocker.vue.d.ts +2 -0
- package/dist/runtime/components/SessionStatus.d.vue.ts +35 -0
- package/dist/runtime/components/SessionStatus.vue +85 -0
- package/dist/runtime/components/SessionStatus.vue.d.ts +35 -0
- package/dist/runtime/components/UserRegistration.d.vue.ts +35 -0
- package/dist/runtime/components/UserRegistration.vue +158 -0
- package/dist/runtime/components/UserRegistration.vue.d.ts +35 -0
- package/dist/runtime/composables/useAuth.d.ts +27 -0
- package/dist/runtime/composables/useAuth.js +73 -0
- package/dist/runtime/core/providerStyles.d.ts +2 -0
- package/dist/runtime/core/providerStyles.js +6 -0
- package/dist/runtime/core/providers/github.d.ts +89 -0
- package/dist/runtime/core/providers/github.js +60 -0
- package/dist/runtime/core/providers/google.d.ts +70 -0
- package/dist/runtime/core/providers/google.js +54 -0
- package/dist/runtime/middleware/authGlobal.d.ts +2 -0
- package/dist/runtime/middleware/authGlobal.js +34 -0
- package/dist/runtime/middleware/authProtected.d.ts +2 -0
- package/dist/runtime/middleware/authProtected.js +38 -0
- package/dist/runtime/server/helpers/getSafeSecretsInfo.d.ts +4 -0
- package/dist/runtime/server/helpers/getSafeSecretsInfo.js +10 -0
- package/dist/runtime/server/helpers/logSafeRoute.d.ts +1 -0
- package/dist/runtime/server/helpers/logSafeRoute.js +3 -0
- package/dist/runtime/server/helpers/signJwt.d.ts +5 -0
- package/dist/runtime/server/helpers/signJwt.js +16 -0
- package/dist/runtime/server/helpers/verifyJwt.d.ts +5 -0
- package/dist/runtime/server/helpers/verifyJwt.js +19 -0
- package/dist/runtime/server/tsconfig.json +3 -0
- package/dist/runtime/server/utils/Auth.d.ts +83 -0
- package/dist/runtime/server/utils/Auth.js +653 -0
- package/dist/runtime/server/utils/SessionManager.d.ts +25 -0
- package/dist/runtime/server/utils/SessionManager.js +93 -0
- package/dist/runtime/server/utils/createAuthHandler.d.ts +8 -0
- package/dist/runtime/server/utils/createAuthHandler.js +29 -0
- package/dist/runtime/server/utils/createAuthMiddleware.d.ts +10 -0
- package/dist/runtime/server/utils/createAuthMiddleware.js +28 -0
- package/dist/runtime/server/utils/createAuthSchema.d.ts +244 -0
- package/dist/runtime/server/utils/createAuthSchema.js +43 -0
- package/dist/runtime/types.d.ts +239 -0
- package/dist/runtime/types.js +15 -0
- package/dist/runtime/utils/createExternalAuthHandler.d.ts +36 -0
- package/dist/runtime/utils/createExternalAuthHandler.js +25 -0
- package/dist/runtime/utils/decodeQueryUri.d.ts +2 -0
- package/dist/runtime/utils/decodeQueryUri.js +4 -0
- package/dist/runtime/utils/getAuthApiRoute.d.ts +3 -0
- package/dist/runtime/utils/getAuthApiRoute.js +14 -0
- package/dist/types.d.mts +13 -0
- package/package.json +101 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<div class="flex flex-col gap-1">
|
|
3
|
+
<span>Bypass auth using {{ provider }} provider as user: </span>
|
|
4
|
+
<WSimpleInput
|
|
5
|
+
class="bg-bg dark:bg-neutral-800"
|
|
6
|
+
label="Username"
|
|
7
|
+
v-model="username"
|
|
8
|
+
/>
|
|
9
|
+
<WCheckbox
|
|
10
|
+
class="bg-bg dark:bg-neutral-800 dark:checked:after:border-neutral-800"
|
|
11
|
+
label="Bypass Registration for New Users"
|
|
12
|
+
v-model="bypassRegistration"
|
|
13
|
+
/>
|
|
14
|
+
<WButton @click="submit">
|
|
15
|
+
Submit
|
|
16
|
+
</WButton>
|
|
17
|
+
</div>
|
|
18
|
+
</template>
|
|
19
|
+
|
|
20
|
+
<script setup>
|
|
21
|
+
import { navigateTo, useRoute } from "#app";
|
|
22
|
+
import { ref } from "#imports";
|
|
23
|
+
import { getAuthApiRoute } from "../utils/getAuthApiRoute.js";
|
|
24
|
+
const username = ref("");
|
|
25
|
+
const bypassRegistration = ref(false);
|
|
26
|
+
const provider = useRoute().query.provider;
|
|
27
|
+
if (!provider || typeof provider !== "string") throw new Error("Missing provider query param.");
|
|
28
|
+
async function submit() {
|
|
29
|
+
const query = useRoute().query;
|
|
30
|
+
delete query.provider;
|
|
31
|
+
const route = getAuthApiRoute(useRuntimeConfig().public, "callback", { provider }, {
|
|
32
|
+
devBypass: true,
|
|
33
|
+
username: username.value,
|
|
34
|
+
devBypassRegistration: bypassRegistration.value,
|
|
35
|
+
...query
|
|
36
|
+
});
|
|
37
|
+
await navigateTo(route, { external: true });
|
|
38
|
+
}
|
|
39
|
+
</script>
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
declare const _default: import("vue").DefineComponent<{}, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<{}> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
|
|
2
|
+
export default _default;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { AuthUser } from "../types.js.js";
|
|
2
|
+
type __VLS_Props = {
|
|
3
|
+
/** The link to the user's profile. Links to `/users/:username` (note this is not provided by the module). */
|
|
4
|
+
usernameLink?: (user: AuthUser) => string;
|
|
5
|
+
};
|
|
6
|
+
declare var __VLS_1: {
|
|
7
|
+
user: any;
|
|
8
|
+
authRoutes: any;
|
|
9
|
+
}, __VLS_3: {
|
|
10
|
+
user: any;
|
|
11
|
+
}, __VLS_18: {
|
|
12
|
+
user: any;
|
|
13
|
+
authRoutes: any;
|
|
14
|
+
}, __VLS_33: {
|
|
15
|
+
authRoutes: any;
|
|
16
|
+
};
|
|
17
|
+
type __VLS_Slots = {} & {
|
|
18
|
+
'authenticated-registered'?: (props: typeof __VLS_1) => any;
|
|
19
|
+
} & {
|
|
20
|
+
user?: (props: typeof __VLS_3) => any;
|
|
21
|
+
} & {
|
|
22
|
+
'authenticated-unregistered'?: (props: typeof __VLS_18) => any;
|
|
23
|
+
} & {
|
|
24
|
+
unauthenticated?: (props: typeof __VLS_33) => any;
|
|
25
|
+
};
|
|
26
|
+
declare const __VLS_component: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {
|
|
27
|
+
usernameLink: (user: AuthUser) => string;
|
|
28
|
+
}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
|
|
29
|
+
declare const _default: __VLS_WithSlots<typeof __VLS_component, __VLS_Slots>;
|
|
30
|
+
export default _default;
|
|
31
|
+
type __VLS_WithSlots<T, S> = T & {
|
|
32
|
+
new (): {
|
|
33
|
+
$slots: S;
|
|
34
|
+
};
|
|
35
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<div v-if="isAuthenticated && user">
|
|
3
|
+
<slot
|
|
4
|
+
v-if="isAuthenticated"
|
|
5
|
+
name="authenticated-registered"
|
|
6
|
+
:user="user"
|
|
7
|
+
:auth-routes="authRoutes"
|
|
8
|
+
>
|
|
9
|
+
<div class="flex gap-4">
|
|
10
|
+
<slot
|
|
11
|
+
name="user"
|
|
12
|
+
:user="user"
|
|
13
|
+
>
|
|
14
|
+
<NuxtLink
|
|
15
|
+
:class="`
|
|
16
|
+
text-underline
|
|
17
|
+
text-l
|
|
18
|
+
${isSemiAuthed ? 'text-red-500' : ''}
|
|
19
|
+
`"
|
|
20
|
+
:title="isSemiAuthed ? 'You need to re-authenticate to use some parts of the app.' : ''"
|
|
21
|
+
:to="usernameLink ? usernameLink(user) : defaultUsernameLink(user)"
|
|
22
|
+
>
|
|
23
|
+
{{ "username" in user ? user.username : user }}
|
|
24
|
+
</NuxtLink>
|
|
25
|
+
</slot>
|
|
26
|
+
<WButton
|
|
27
|
+
:unstyle="true"
|
|
28
|
+
class=""
|
|
29
|
+
@click="logout()"
|
|
30
|
+
>
|
|
31
|
+
Logout
|
|
32
|
+
</WButton>
|
|
33
|
+
</div>
|
|
34
|
+
</slot>
|
|
35
|
+
<slot
|
|
36
|
+
v-else
|
|
37
|
+
name="authenticated-unregistered"
|
|
38
|
+
:user="user"
|
|
39
|
+
:auth-routes="authRoutes"
|
|
40
|
+
>
|
|
41
|
+
<div class="flex gap-4">
|
|
42
|
+
<NuxtLink
|
|
43
|
+
:to="authRoutes.register"
|
|
44
|
+
>
|
|
45
|
+
Register
|
|
46
|
+
</NuxtLink>
|
|
47
|
+
<WButton
|
|
48
|
+
:unstyle="true"
|
|
49
|
+
@click="logout()"
|
|
50
|
+
>
|
|
51
|
+
Logout
|
|
52
|
+
</WButton>
|
|
53
|
+
</div>
|
|
54
|
+
</slot>
|
|
55
|
+
</div>
|
|
56
|
+
<div v-else>
|
|
57
|
+
<slot
|
|
58
|
+
name="unauthenticated"
|
|
59
|
+
:auth-routes="authRoutes"
|
|
60
|
+
>
|
|
61
|
+
<NuxtLink
|
|
62
|
+
:to="authRoutes.login"
|
|
63
|
+
>
|
|
64
|
+
Login / Register
|
|
65
|
+
</NuxtLink>
|
|
66
|
+
</slot>
|
|
67
|
+
</div>
|
|
68
|
+
</template>
|
|
69
|
+
|
|
70
|
+
<script setup>
|
|
71
|
+
import { useAuth } from "../composables/useAuth";
|
|
72
|
+
const {
|
|
73
|
+
logout,
|
|
74
|
+
user,
|
|
75
|
+
authRoutes,
|
|
76
|
+
isAuthenticated,
|
|
77
|
+
isSemiAuthed
|
|
78
|
+
} = useAuth();
|
|
79
|
+
function defaultUsernameLink(user2) {
|
|
80
|
+
return isSemiAuthed.value ? useAuth().authRoutes.login : `/users/${"username" in user2 ? user2.username : ""}`;
|
|
81
|
+
}
|
|
82
|
+
defineProps({
|
|
83
|
+
usernameLink: { type: Function, required: false, default: void 0 }
|
|
84
|
+
});
|
|
85
|
+
</script>
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { AuthUser } from "../types.js.js";
|
|
2
|
+
type __VLS_Props = {
|
|
3
|
+
/** The link to the user's profile. Links to `/users/:username` (note this is not provided by the module). */
|
|
4
|
+
usernameLink?: (user: AuthUser) => string;
|
|
5
|
+
};
|
|
6
|
+
declare var __VLS_1: {
|
|
7
|
+
user: any;
|
|
8
|
+
authRoutes: any;
|
|
9
|
+
}, __VLS_3: {
|
|
10
|
+
user: any;
|
|
11
|
+
}, __VLS_18: {
|
|
12
|
+
user: any;
|
|
13
|
+
authRoutes: any;
|
|
14
|
+
}, __VLS_33: {
|
|
15
|
+
authRoutes: any;
|
|
16
|
+
};
|
|
17
|
+
type __VLS_Slots = {} & {
|
|
18
|
+
'authenticated-registered'?: (props: typeof __VLS_1) => any;
|
|
19
|
+
} & {
|
|
20
|
+
user?: (props: typeof __VLS_3) => any;
|
|
21
|
+
} & {
|
|
22
|
+
'authenticated-unregistered'?: (props: typeof __VLS_18) => any;
|
|
23
|
+
} & {
|
|
24
|
+
unauthenticated?: (props: typeof __VLS_33) => any;
|
|
25
|
+
};
|
|
26
|
+
declare const __VLS_component: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {
|
|
27
|
+
usernameLink: (user: AuthUser) => string;
|
|
28
|
+
}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
|
|
29
|
+
declare const _default: __VLS_WithSlots<typeof __VLS_component, __VLS_Slots>;
|
|
30
|
+
export default _default;
|
|
31
|
+
type __VLS_WithSlots<T, S> = T & {
|
|
32
|
+
new (): {
|
|
33
|
+
$slots: S;
|
|
34
|
+
};
|
|
35
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type Ref } from "#imports";
|
|
2
|
+
type __VLS_Props = {
|
|
3
|
+
debounce?: number;
|
|
4
|
+
/** Can throw to indicate the username is invalid (before a request to properly check it is even made. The default throws if the default username schema isn't met. */
|
|
5
|
+
getValidUsernameRoute?: (username: string) => string;
|
|
6
|
+
onSubmitRegistration?: (event: Event, username: string, error: Ref<string>, redirectUrl: string, deeplink?: string) => void;
|
|
7
|
+
id?: string;
|
|
8
|
+
};
|
|
9
|
+
declare var __VLS_17: {}, __VLS_24: {}, __VLS_31: {}, __VLS_38: {}, __VLS_45: {
|
|
10
|
+
error: any;
|
|
11
|
+
registrationError: any;
|
|
12
|
+
};
|
|
13
|
+
type __VLS_Slots = {} & {
|
|
14
|
+
'username-icon-loading'?: (props: typeof __VLS_17) => any;
|
|
15
|
+
} & {
|
|
16
|
+
'username-icon-valid'?: (props: typeof __VLS_24) => any;
|
|
17
|
+
} & {
|
|
18
|
+
'username-icon-invalid'?: (props: typeof __VLS_31) => any;
|
|
19
|
+
} & {
|
|
20
|
+
default?: (props: typeof __VLS_38) => any;
|
|
21
|
+
} & {
|
|
22
|
+
error?: (props: typeof __VLS_45) => any;
|
|
23
|
+
};
|
|
24
|
+
declare const __VLS_component: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {
|
|
25
|
+
debounce: number;
|
|
26
|
+
getValidUsernameRoute: (username: string) => string;
|
|
27
|
+
onSubmitRegistration: (event: Event, username: string, error: Ref<string>, redirectUrl: string, deeplink?: string) => void;
|
|
28
|
+
}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
|
|
29
|
+
declare const _default: __VLS_WithSlots<typeof __VLS_component, __VLS_Slots>;
|
|
30
|
+
export default _default;
|
|
31
|
+
type __VLS_WithSlots<T, S> = T & {
|
|
32
|
+
new (): {
|
|
33
|
+
$slots: S;
|
|
34
|
+
};
|
|
35
|
+
};
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<form
|
|
3
|
+
class="
|
|
4
|
+
flex-1
|
|
5
|
+
flex
|
|
6
|
+
flex-col
|
|
7
|
+
justify-center
|
|
8
|
+
gap-4
|
|
9
|
+
w-[500px]
|
|
10
|
+
max-w-full
|
|
11
|
+
p-10
|
|
12
|
+
"
|
|
13
|
+
@submit.prevent="handleSubmit"
|
|
14
|
+
>
|
|
15
|
+
<div class="flex flex-col">
|
|
16
|
+
<WLabel
|
|
17
|
+
:for="usernameId"
|
|
18
|
+
>
|
|
19
|
+
Username
|
|
20
|
+
</WLabel>
|
|
21
|
+
<div>
|
|
22
|
+
<WInputDeprecated
|
|
23
|
+
:id="usernameId"
|
|
24
|
+
name="username"
|
|
25
|
+
class="w-0"
|
|
26
|
+
inner-wrapper-class="bg-bg dark:bg-neutral-800"
|
|
27
|
+
v-model="username"
|
|
28
|
+
>
|
|
29
|
+
<template #right>
|
|
30
|
+
<WIcon class="w-[1rem] pointer-events-none">
|
|
31
|
+
<slot
|
|
32
|
+
v-if="isLoading"
|
|
33
|
+
name="username-icon-loading"
|
|
34
|
+
>
|
|
35
|
+
<IconSpinner
|
|
36
|
+
class="animate-spin text-neutral-500"
|
|
37
|
+
/>
|
|
38
|
+
</slot>
|
|
39
|
+
<slot
|
|
40
|
+
v-else-if="isValidUsername"
|
|
41
|
+
name="username-icon-valid"
|
|
42
|
+
>
|
|
43
|
+
<IconCheck
|
|
44
|
+
class="text-green-500"
|
|
45
|
+
/>
|
|
46
|
+
</slot>
|
|
47
|
+
<slot
|
|
48
|
+
v-else-if="anyError || !isValidUsername && username !== ''"
|
|
49
|
+
name="username-icon-invalid"
|
|
50
|
+
>
|
|
51
|
+
<IconInvalid
|
|
52
|
+
class="text-red-500"
|
|
53
|
+
/>
|
|
54
|
+
</slot>
|
|
55
|
+
</WIcon>
|
|
56
|
+
</template>
|
|
57
|
+
</WInputDeprecated>
|
|
58
|
+
</div>
|
|
59
|
+
</div>
|
|
60
|
+
<slot/>
|
|
61
|
+
<WButton
|
|
62
|
+
:id="submitId"
|
|
63
|
+
:disabled="!!error || isLoading || username === '' || !isValidUsername"
|
|
64
|
+
class="w-full"
|
|
65
|
+
type="submit"
|
|
66
|
+
>
|
|
67
|
+
Register
|
|
68
|
+
</WButton>
|
|
69
|
+
<slot
|
|
70
|
+
v-if="anyError"
|
|
71
|
+
name="error"
|
|
72
|
+
v-bind="{ error, registrationError }"
|
|
73
|
+
>
|
|
74
|
+
<div
|
|
75
|
+
class="
|
|
76
|
+
border
|
|
77
|
+
border-red-500
|
|
78
|
+
rounded-md
|
|
79
|
+
p-2
|
|
80
|
+
text-red-500
|
|
81
|
+
bg-red-10j0
|
|
82
|
+
dark:bg-red-900
|
|
83
|
+
whitespace-pre-wrap
|
|
84
|
+
break-all
|
|
85
|
+
"
|
|
86
|
+
>
|
|
87
|
+
{{ registrationError || error }}
|
|
88
|
+
</div>
|
|
89
|
+
</slot>
|
|
90
|
+
</form>
|
|
91
|
+
</template>
|
|
92
|
+
|
|
93
|
+
<script setup>
|
|
94
|
+
import { refDebounced } from "@vueuse/core";
|
|
95
|
+
import { navigateTo, useAsyncData, useRuntimeConfig } from "#app";
|
|
96
|
+
import { computed, ref, useId, useRoute } from "#imports";
|
|
97
|
+
import IconInvalid from "~icons/fa-solid/times";
|
|
98
|
+
import IconCheck from "~icons/fa6-solid/check";
|
|
99
|
+
import IconSpinner from "~icons/gg/spinner";
|
|
100
|
+
import { useAuth } from "../composables/useAuth.js";
|
|
101
|
+
import { AUTH_ERROR, defaultZodUsernameSchema } from "../types.js";
|
|
102
|
+
import { getAuthApiRoute } from "../utils/getAuthApiRoute.js";
|
|
103
|
+
const query = useRoute().query;
|
|
104
|
+
const submitId = useId();
|
|
105
|
+
const props = defineProps({
|
|
106
|
+
debounce: { type: Number, required: false, default: 1e3 },
|
|
107
|
+
getValidUsernameRoute: { type: Function, required: false, default: (username2) => {
|
|
108
|
+
const res = defaultZodUsernameSchema.safeParse(username2);
|
|
109
|
+
if (!res.success) throw new Error(res.error.format()._errors.join("\n"));
|
|
110
|
+
return getAuthApiRoute(useRuntimeConfig().public, "usernameValid", { username: username2 });
|
|
111
|
+
} },
|
|
112
|
+
onSubmitRegistration: { type: Function, required: false, default: async (_event, username2, error2, redirectUrl, deeplink2) => {
|
|
113
|
+
const res = await $fetch(`${getAuthApiRoute(useRuntimeConfig().public, "register")}${deeplink2 ? `?deeplink=${deeplink2}` : ""}`, {
|
|
114
|
+
cache: "no-store",
|
|
115
|
+
method: "post",
|
|
116
|
+
body: {
|
|
117
|
+
username: username2
|
|
118
|
+
}
|
|
119
|
+
}).catch((e) => {
|
|
120
|
+
if (e?.data?.code === AUTH_ERROR.USER_ALREADY_REGISTERED) {
|
|
121
|
+
useAuth().setFetchUserOnNavigation(true);
|
|
122
|
+
navigateTo(redirectUrl, { external: true });
|
|
123
|
+
}
|
|
124
|
+
error2.value = `Registration Error: ${e.data.message}`;
|
|
125
|
+
});
|
|
126
|
+
if (typeof res === "object" && res.redirectUrl) {
|
|
127
|
+
navigateTo(res.redirectUrl, { external: true });
|
|
128
|
+
}
|
|
129
|
+
} },
|
|
130
|
+
id: { type: String, required: false }
|
|
131
|
+
});
|
|
132
|
+
const usernameId = props.id ?? useId();
|
|
133
|
+
const username = ref("");
|
|
134
|
+
const debouncedUsername = refDebounced(username, props.debounce);
|
|
135
|
+
const registrationError = ref("");
|
|
136
|
+
const { data: isValidUsername, status, error } = await useAsyncData(
|
|
137
|
+
"auth:username:valid",
|
|
138
|
+
() => $fetch(props.getValidUsernameRoute(debouncedUsername.value)),
|
|
139
|
+
{
|
|
140
|
+
watch: [debouncedUsername],
|
|
141
|
+
immediate: false,
|
|
142
|
+
default: () => false
|
|
143
|
+
}
|
|
144
|
+
);
|
|
145
|
+
const isLoading = computed(() => username.value !== "" && (status.value === "pending" || username.value !== debouncedUsername.value));
|
|
146
|
+
const anyError = computed(() => error.value || registrationError.value);
|
|
147
|
+
const deeplink = typeof query.deeplink === "string" ? query.deeplink : void 0;
|
|
148
|
+
const redirect = deeplink ? useRuntimeConfig().public.auth.authRoutes.externalCode : useRuntimeConfig().public.auth.authRoutes.register;
|
|
149
|
+
function handleSubmit(event) {
|
|
150
|
+
props.onSubmitRegistration(
|
|
151
|
+
event,
|
|
152
|
+
username.value,
|
|
153
|
+
registrationError,
|
|
154
|
+
redirect,
|
|
155
|
+
deeplink
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
</script>
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type Ref } from "#imports";
|
|
2
|
+
type __VLS_Props = {
|
|
3
|
+
debounce?: number;
|
|
4
|
+
/** Can throw to indicate the username is invalid (before a request to properly check it is even made. The default throws if the default username schema isn't met. */
|
|
5
|
+
getValidUsernameRoute?: (username: string) => string;
|
|
6
|
+
onSubmitRegistration?: (event: Event, username: string, error: Ref<string>, redirectUrl: string, deeplink?: string) => void;
|
|
7
|
+
id?: string;
|
|
8
|
+
};
|
|
9
|
+
declare var __VLS_17: {}, __VLS_24: {}, __VLS_31: {}, __VLS_38: {}, __VLS_45: {
|
|
10
|
+
error: any;
|
|
11
|
+
registrationError: any;
|
|
12
|
+
};
|
|
13
|
+
type __VLS_Slots = {} & {
|
|
14
|
+
'username-icon-loading'?: (props: typeof __VLS_17) => any;
|
|
15
|
+
} & {
|
|
16
|
+
'username-icon-valid'?: (props: typeof __VLS_24) => any;
|
|
17
|
+
} & {
|
|
18
|
+
'username-icon-invalid'?: (props: typeof __VLS_31) => any;
|
|
19
|
+
} & {
|
|
20
|
+
default?: (props: typeof __VLS_38) => any;
|
|
21
|
+
} & {
|
|
22
|
+
error?: (props: typeof __VLS_45) => any;
|
|
23
|
+
};
|
|
24
|
+
declare const __VLS_component: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {
|
|
25
|
+
debounce: number;
|
|
26
|
+
getValidUsernameRoute: (username: string) => string;
|
|
27
|
+
onSubmitRegistration: (event: Event, username: string, error: Ref<string>, redirectUrl: string, deeplink?: string) => void;
|
|
28
|
+
}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
|
|
29
|
+
declare const _default: __VLS_WithSlots<typeof __VLS_component, __VLS_Slots>;
|
|
30
|
+
export default _default;
|
|
31
|
+
type __VLS_WithSlots<T, S> = T & {
|
|
32
|
+
new (): {
|
|
33
|
+
$slots: S;
|
|
34
|
+
};
|
|
35
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ProviderNames, UseAuthComposableOptions } from "#auth/types.js";
|
|
2
|
+
type AuthListenerHook = () => void;
|
|
3
|
+
type AuthListenerHooks = "beforeLogout";
|
|
4
|
+
export declare const useAuth: ({ handleActions }?: UseAuthComposableOptions) => {
|
|
5
|
+
on: (hook: AuthListenerHooks, listener: AuthListenerHook) => Promise<void>;
|
|
6
|
+
off: (hook: AuthListenerHooks, listener: AuthListenerHook) => Promise<void>;
|
|
7
|
+
user: import("vue").Ref<import("#auth/types.js").AuthUserFields | null, import("#auth/types.js").AuthUserFields | null>;
|
|
8
|
+
setFetchUserOnNavigation: (value?: boolean) => void;
|
|
9
|
+
login: (provider: ProviderNames, { devBypass }?: {
|
|
10
|
+
/** Send the request with the devBypass query param (This will only actually bypass auth if the server permits it). */
|
|
11
|
+
devBypass?: boolean;
|
|
12
|
+
}) => Promise<void>;
|
|
13
|
+
logout: () => Promise<void>;
|
|
14
|
+
authRoutes: {
|
|
15
|
+
login: string | undefined;
|
|
16
|
+
unauthenticated?: string | undefined;
|
|
17
|
+
register?: string | undefined;
|
|
18
|
+
postRegisteredLogin?: string | undefined;
|
|
19
|
+
externalCode?: string | undefined;
|
|
20
|
+
deeplink?: string | undefined;
|
|
21
|
+
mockAuth?: string | undefined;
|
|
22
|
+
};
|
|
23
|
+
isAuthenticated: import("vue").ComputedRef<boolean>;
|
|
24
|
+
isRegistered: import("vue").ComputedRef<boolean>;
|
|
25
|
+
isSemiAuthed: import("vue").Ref<boolean, boolean>;
|
|
26
|
+
};
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import {
|
|
2
|
+
navigateTo,
|
|
3
|
+
useRuntimeConfig,
|
|
4
|
+
useState
|
|
5
|
+
} from "#app";
|
|
6
|
+
import { computed } from "#imports";
|
|
7
|
+
import { getAuthApiRoute } from "../utils/getAuthApiRoute.js";
|
|
8
|
+
const hooks = {
|
|
9
|
+
beforeLogout: []
|
|
10
|
+
};
|
|
11
|
+
export const useAuth = ({ handleActions } = {}) => {
|
|
12
|
+
const config = useRuntimeConfig().public.auth;
|
|
13
|
+
const doFetch = useState("auth:_fetch");
|
|
14
|
+
const userData = useState("auth:user", () => null);
|
|
15
|
+
const isSemiAuthed = useState("auth:semiAuthed", () => false);
|
|
16
|
+
const isAuthenticated = computed(() => !!userData.value);
|
|
17
|
+
const isRegistered = computed(() => !!userData.value?.isRegistered);
|
|
18
|
+
const authRoutes = {
|
|
19
|
+
...config.authRoutes,
|
|
20
|
+
login: config.authRoutes.login
|
|
21
|
+
};
|
|
22
|
+
function setFetchUserOnNavigation(value = true) {
|
|
23
|
+
doFetch.value = value;
|
|
24
|
+
}
|
|
25
|
+
async function login(provider, {
|
|
26
|
+
devBypass = false
|
|
27
|
+
} = {}) {
|
|
28
|
+
setFetchUserOnNavigation();
|
|
29
|
+
const loginRoute = getAuthApiRoute(useRuntimeConfig().public, "login", { provider: provider.toLowerCase() }, { devBypass });
|
|
30
|
+
const external = provider ? true : void 0;
|
|
31
|
+
let handled = handleActions?.("login", loginRoute, provider);
|
|
32
|
+
if (handled instanceof Promise) handled = await handled;
|
|
33
|
+
if (!handled) {
|
|
34
|
+
await navigateTo(loginRoute, { external });
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
async function logout() {
|
|
38
|
+
setFetchUserOnNavigation();
|
|
39
|
+
userData.value = null;
|
|
40
|
+
await Promise.allSettled(hooks.beforeLogout.map((listener) => listener()));
|
|
41
|
+
const handled = handleActions?.("logout", getAuthApiRoute(useRuntimeConfig().public, "logout"));
|
|
42
|
+
if (!handled) {
|
|
43
|
+
const res = await $fetch(getAuthApiRoute(useRuntimeConfig().public, "logout"), {
|
|
44
|
+
cache: "no-store",
|
|
45
|
+
method: "post"
|
|
46
|
+
});
|
|
47
|
+
if (res) {
|
|
48
|
+
await navigateTo(authRoutes.unauthenticated, { external: true });
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async function on(hook, listener) {
|
|
53
|
+
hooks[hook].push(listener);
|
|
54
|
+
}
|
|
55
|
+
async function off(hook, listener) {
|
|
56
|
+
const index = hooks[hook].indexOf(listener);
|
|
57
|
+
if (index > -1) {
|
|
58
|
+
hooks[hook].splice(index, 1);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
on,
|
|
63
|
+
off,
|
|
64
|
+
user: userData,
|
|
65
|
+
setFetchUserOnNavigation,
|
|
66
|
+
login,
|
|
67
|
+
logout,
|
|
68
|
+
authRoutes,
|
|
69
|
+
isAuthenticated,
|
|
70
|
+
isRegistered,
|
|
71
|
+
isSemiAuthed
|
|
72
|
+
};
|
|
73
|
+
};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
declare module "../../types.js" {
|
|
2
|
+
interface InternalProviders {
|
|
3
|
+
github: GithubUser;
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
export interface GithubUser {
|
|
7
|
+
login: string;
|
|
8
|
+
id: number;
|
|
9
|
+
node_id: string;
|
|
10
|
+
avatar_url: string;
|
|
11
|
+
gravatar_id: string | null;
|
|
12
|
+
url: string;
|
|
13
|
+
html_url: string;
|
|
14
|
+
followers_url: string;
|
|
15
|
+
following_url: string;
|
|
16
|
+
gists_url: string;
|
|
17
|
+
starred_url: string;
|
|
18
|
+
subscriptions_url: string;
|
|
19
|
+
organizations_url: string;
|
|
20
|
+
repos_url: string;
|
|
21
|
+
events_url: string;
|
|
22
|
+
received_events_url: string;
|
|
23
|
+
type: string;
|
|
24
|
+
site_admin: boolean;
|
|
25
|
+
name: string | null;
|
|
26
|
+
company: string | null;
|
|
27
|
+
blog: string | null;
|
|
28
|
+
location: string | null;
|
|
29
|
+
email: string | null;
|
|
30
|
+
notification_email?: string | null;
|
|
31
|
+
hireable: boolean | null;
|
|
32
|
+
bio: string | null;
|
|
33
|
+
twitter_username?: string | null;
|
|
34
|
+
public_repos: number;
|
|
35
|
+
public_gists: number;
|
|
36
|
+
followers: number;
|
|
37
|
+
following: number;
|
|
38
|
+
created_at: string;
|
|
39
|
+
updated_at: string;
|
|
40
|
+
private_gists: number;
|
|
41
|
+
total_private_repos: number;
|
|
42
|
+
owned_private_repos: number;
|
|
43
|
+
disk_usage: number;
|
|
44
|
+
collaborators: number;
|
|
45
|
+
two_factor_authentication: boolean;
|
|
46
|
+
plan?: {
|
|
47
|
+
collaborators: number;
|
|
48
|
+
name: string;
|
|
49
|
+
space: number;
|
|
50
|
+
private_repos: number;
|
|
51
|
+
[k: string]: unknown;
|
|
52
|
+
};
|
|
53
|
+
suspended_at?: string | null;
|
|
54
|
+
business_plus?: boolean;
|
|
55
|
+
ldap_dn?: string;
|
|
56
|
+
[k: string]: unknown;
|
|
57
|
+
}
|
|
58
|
+
import { GitHub, type OAuth2Tokens } from "arctic";
|
|
59
|
+
import type { ProviderHandler, ProviderHandlerOptions, ProviderOptions } from "../../types.js";
|
|
60
|
+
export default class GithubProvider implements ProviderHandler<"oauth2", "github", GitHub> {
|
|
61
|
+
name: "github";
|
|
62
|
+
type: "oauth2";
|
|
63
|
+
provider: GitHub;
|
|
64
|
+
providerOptions: ProviderHandlerOptions;
|
|
65
|
+
options: Partial<ProviderOptions<"github">>;
|
|
66
|
+
constructor(providerOptions: GithubProvider["providerOptions"], options?: GithubProvider["options"]);
|
|
67
|
+
getLoginInfo(state: string): {
|
|
68
|
+
type: "oauth2";
|
|
69
|
+
url: URL;
|
|
70
|
+
};
|
|
71
|
+
getAccountInfo(tokens: OAuth2Tokens): Promise<{
|
|
72
|
+
info: null;
|
|
73
|
+
name: string;
|
|
74
|
+
email: string;
|
|
75
|
+
providerId: string;
|
|
76
|
+
provider: string;
|
|
77
|
+
isVerified: boolean;
|
|
78
|
+
}>;
|
|
79
|
+
}
|
|
80
|
+
export declare const githubProviderStyle: {
|
|
81
|
+
name: string;
|
|
82
|
+
logo: import("vue").FunctionalComponent<import("vue").SVGAttributes, {}, any, {}>;
|
|
83
|
+
style: {
|
|
84
|
+
bg: string;
|
|
85
|
+
bgDark: string;
|
|
86
|
+
text: string;
|
|
87
|
+
textDark: string;
|
|
88
|
+
};
|
|
89
|
+
};
|