@witchcraft/nuxt-auth 0.4.1 → 0.5.0
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/dist/module.d.mts +1 -1
- package/dist/module.json +1 -1
- package/dist/module.mjs +1 -1
- package/dist/runtime/components/ExternalCallback.d.vue.ts +2 -6
- package/dist/runtime/components/ExternalCallback.vue +2 -37
- package/dist/runtime/components/ExternalCallback.vue.d.ts +2 -6
- package/dist/runtime/components/ExternalCode.vue +3 -20
- package/dist/runtime/components/LoginProviderButtons.client.d.vue.ts +17 -14
- package/dist/runtime/components/LoginProviderButtons.client.vue +26 -56
- package/dist/runtime/components/LoginProviderButtons.client.vue.d.ts +17 -14
- package/dist/runtime/components/Mocker.vue +2 -18
- package/dist/runtime/components/UserRegistration.d.vue.ts +15 -39
- package/dist/runtime/components/UserRegistration.vue +40 -144
- package/dist/runtime/components/UserRegistration.vue.d.ts +15 -39
- package/dist/runtime/composables/useAuthExternalCallback.d.ts +17 -0
- package/dist/runtime/composables/useAuthExternalCallback.js +47 -0
- package/dist/runtime/composables/useAuthExternalCode.d.ts +5 -0
- package/dist/runtime/composables/useAuthExternalCode.js +29 -0
- package/dist/runtime/composables/useAuthMocker.d.ts +7 -0
- package/dist/runtime/composables/useAuthMocker.js +33 -0
- package/dist/runtime/composables/useAuthUserRegistration.d.ts +43 -0
- package/dist/runtime/composables/useAuthUserRegistration.js +84 -0
- package/dist/runtime/core/providers/github.d.ts +1 -6
- package/dist/runtime/core/providers/github.js +1 -6
- package/dist/runtime/core/providers/google.d.ts +1 -6
- package/dist/runtime/core/providers/google.js +1 -6
- package/dist/runtime/server/helpers/signJwt.d.ts +1 -3
- package/dist/runtime/server/utils/Auth.d.ts +2 -1
- package/dist/runtime/server/utils/Auth.js +21 -6
- package/dist/runtime/server/utils/SessionManager.d.ts +1 -1
- package/dist/runtime/server/utils/SessionManager.js +6 -5
- package/dist/runtime/server/utils/createAuthMiddleware.js +2 -2
- package/dist/runtime/types.d.ts +1 -7
- package/package.json +6 -3
package/dist/module.d.mts
CHANGED
|
@@ -143,7 +143,7 @@ interface ModuleOptions {
|
|
|
143
143
|
/**
|
|
144
144
|
* Note the route is NOT defined by the module, you must extend the routes and define it yourself if you are using the UserRegistration component which makes use of it.
|
|
145
145
|
*
|
|
146
|
-
* @default "/api/auth/
|
|
146
|
+
* @default "/api/auth/users/username/valid"
|
|
147
147
|
*/
|
|
148
148
|
usernameValid: string;
|
|
149
149
|
}> & Partial<AdditionalApiRoutes>;
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -44,7 +44,7 @@ const module$1 = defineNuxtModule({
|
|
|
44
44
|
login: "/login/:provider",
|
|
45
45
|
callback: "/callback/:provider",
|
|
46
46
|
register: "/register",
|
|
47
|
-
usernameValid: "/
|
|
47
|
+
usernameValid: "/users/username/valid"
|
|
48
48
|
},
|
|
49
49
|
onlySaveUnregisteredUserAccountInfo: false,
|
|
50
50
|
additionalMiddlewarePaths: {}
|
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
type
|
|
2
|
-
|
|
3
|
-
successPath?: string;
|
|
4
|
-
cancelPath?: string;
|
|
5
|
-
};
|
|
6
|
-
declare const __VLS_export: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
|
|
1
|
+
import type { ExternalCallbackOptions } from "../composables/useAuthExternalCallback.js";
|
|
2
|
+
declare const __VLS_export: import("vue").DefineComponent<ExternalCallbackOptions, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<ExternalCallbackOptions> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
|
|
7
3
|
declare const _default: typeof __VLS_export;
|
|
8
4
|
export default _default;
|
|
@@ -55,46 +55,11 @@
|
|
|
55
55
|
</template>
|
|
56
56
|
|
|
57
57
|
<script setup>
|
|
58
|
-
import
|
|
59
|
-
import { useRuntimeConfig } from "#app";
|
|
60
|
-
import {
|
|
61
|
-
navigateTo,
|
|
62
|
-
ref,
|
|
63
|
-
useRoute
|
|
64
|
-
} from "#imports";
|
|
65
|
-
import { zExternalCallbackPageQuery } from "../types.js";
|
|
58
|
+
import { useAuthExternalCallback } from "../composables/useAuthExternalCallback.js";
|
|
66
59
|
const props = defineProps({
|
|
67
60
|
saveSession: { type: Function, required: true },
|
|
68
61
|
successPath: { type: String, required: false },
|
|
69
62
|
cancelPath: { type: String, required: false }
|
|
70
63
|
});
|
|
71
|
-
const
|
|
72
|
-
const query = useRoute().query;
|
|
73
|
-
const parsedQuery = zExternalCallbackPageQuery.safeParse(query);
|
|
74
|
-
if (parsedQuery.error) throw new Error(z.prettifyError(parsedQuery.error));
|
|
75
|
-
const initialAccessToken = "accessToken" in parsedQuery.data ? parsedQuery.data.accessToken : void 0;
|
|
76
|
-
const authUri = "authUri" in parsedQuery.data ? parsedQuery.data.authUri : void 0;
|
|
77
|
-
if (initialAccessToken) {
|
|
78
|
-
void authorize(initialAccessToken, true);
|
|
79
|
-
}
|
|
80
|
-
const manualAccessToken = ref("");
|
|
81
|
-
const error = ref();
|
|
82
|
-
async function authorize(accessToken, initial = false) {
|
|
83
|
-
if (!accessToken || accessToken.length === 0) {
|
|
84
|
-
if (!initial) {
|
|
85
|
-
error.value = "No code provided.";
|
|
86
|
-
}
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
const res = await props.saveSession(accessToken).catch((err) => {
|
|
90
|
-
error.value = err.message;
|
|
91
|
-
return err;
|
|
92
|
-
});
|
|
93
|
-
if (!(res instanceof Error)) {
|
|
94
|
-
await navigateTo(props.successPath ?? rc.public.auth.authRoutes.postRegisteredLogin);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
async function cancel() {
|
|
98
|
-
await navigateTo(props.cancelPath ?? rc.public.auth.authRoutes.login);
|
|
99
|
-
}
|
|
64
|
+
const { authUri, initialAccessToken, manualAccessToken, error, authorize, cancel } = useAuthExternalCallback(props);
|
|
100
65
|
</script>
|
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
type
|
|
2
|
-
|
|
3
|
-
successPath?: string;
|
|
4
|
-
cancelPath?: string;
|
|
5
|
-
};
|
|
6
|
-
declare const __VLS_export: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
|
|
1
|
+
import type { ExternalCallbackOptions } from "../composables/useAuthExternalCallback.js";
|
|
2
|
+
declare const __VLS_export: import("vue").DefineComponent<ExternalCallbackOptions, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<ExternalCallbackOptions> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
|
|
7
3
|
declare const _default: typeof __VLS_export;
|
|
8
4
|
export default _default;
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
hover:text-accent-500
|
|
40
40
|
break-all
|
|
41
41
|
"
|
|
42
|
-
@click="
|
|
42
|
+
@click="copyToken"
|
|
43
43
|
>
|
|
44
44
|
{{ accessToken }}
|
|
45
45
|
</WButton>
|
|
@@ -47,28 +47,11 @@
|
|
|
47
47
|
</template>
|
|
48
48
|
|
|
49
49
|
<script setup>
|
|
50
|
-
import {
|
|
51
|
-
|
|
52
|
-
import {
|
|
53
|
-
navigateTo,
|
|
54
|
-
onMounted,
|
|
55
|
-
useRoute
|
|
56
|
-
} from "#imports";
|
|
57
|
-
import { zUriComponentCodec } from "../types.js";
|
|
58
|
-
const query = useRoute().query;
|
|
59
|
-
const accessToken = query.accessToken;
|
|
60
|
-
if (typeof accessToken !== "string") {
|
|
61
|
-
unreachable("No session_token.");
|
|
62
|
-
}
|
|
63
|
-
const deepLinkUrl = zUriComponentCodec.parse(query.deeplinkUri);
|
|
50
|
+
import { useAuthExternalCode } from "../composables/useAuthExternalCode.js";
|
|
51
|
+
const { accessToken, deepLinkUrl, copyToken } = useAuthExternalCode();
|
|
64
52
|
defineProps({
|
|
65
53
|
openAppText: { type: String, required: false, default: "Open App and Authorize" },
|
|
66
54
|
promptText: { type: String, required: false, default: "If you are not prompted to open the app, try clicking below or copy this token into the app:" },
|
|
67
55
|
copyTitle: { type: String, required: false, default: "Click to Copy" }
|
|
68
56
|
});
|
|
69
|
-
onMounted(() => {
|
|
70
|
-
if (accessToken) {
|
|
71
|
-
void navigateTo(deepLinkUrl, { external: true });
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
57
|
</script>
|
|
@@ -1,27 +1,30 @@
|
|
|
1
1
|
import { useAuth } from "../composables/useAuth.js";
|
|
2
2
|
import type { ProviderNames, ProviderStyle, UseAuthComposableOptions } from "../types.js";
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* @deprecated This component is deprecated and will be removed in a future version.
|
|
5
|
+
*/
|
|
6
|
+
declare const _default: typeof __VLS_export;
|
|
7
|
+
export default _default;
|
|
8
|
+
declare const __VLS_export: __VLS_WithSlots<import("vue").DefineComponent<{
|
|
4
9
|
providerStyles?: Record<ProviderNames, Partial<ProviderStyle>>;
|
|
5
10
|
useAuthOptions?: UseAuthComposableOptions;
|
|
6
11
|
loginOptions?: Parameters<ReturnType<typeof useAuth>["login"]>[1];
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
type __VLS_Slots = {} & {
|
|
13
|
-
extra?: (props: typeof __VLS_20) => any;
|
|
14
|
-
};
|
|
15
|
-
declare const __VLS_base: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {
|
|
12
|
+
}, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<{
|
|
13
|
+
providerStyles?: Record<ProviderNames, Partial<ProviderStyle>>;
|
|
14
|
+
useAuthOptions?: UseAuthComposableOptions;
|
|
15
|
+
loginOptions?: Parameters<ReturnType<typeof useAuth>["login"]>[1];
|
|
16
|
+
}> & Readonly<{}>, {
|
|
16
17
|
providerStyles: Record<ProviderNames, Partial<ProviderStyle>>;
|
|
17
18
|
useAuthOptions: UseAuthComposableOptions;
|
|
18
19
|
loginOptions: {
|
|
19
20
|
devBypass?: boolean;
|
|
20
21
|
};
|
|
21
|
-
}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>, {
|
|
23
|
+
extra?: (props: {
|
|
24
|
+
class: string;
|
|
25
|
+
iconClass: string;
|
|
26
|
+
}) => any;
|
|
27
|
+
}>;
|
|
25
28
|
type __VLS_WithSlots<T, S> = T & {
|
|
26
29
|
new (): {
|
|
27
30
|
$slots: S;
|
|
@@ -1,75 +1,45 @@
|
|
|
1
1
|
<template>
|
|
2
|
-
<
|
|
3
|
-
:class="
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
items-stretch
|
|
8
|
-
justify-center
|
|
9
|
-
gap-2
|
|
10
|
-
`, $attrs?.class)"
|
|
2
|
+
<WAuth
|
|
3
|
+
:class="$attrs?.class"
|
|
4
|
+
:providers="enabledProviders"
|
|
5
|
+
:provider-styles="providerStyles"
|
|
6
|
+
@login="onLogin"
|
|
11
7
|
>
|
|
12
|
-
<template
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
I think because of the for loop -->
|
|
18
|
-
<WButton
|
|
19
|
-
v-if="provider"
|
|
20
|
-
:id="'login-provider-' + provider"
|
|
21
|
-
type="button"
|
|
22
|
-
class="text-l p-2 px-4 [&_label]:justify-start [&_label]:gap-4"
|
|
23
|
-
:style="styles[provider]"
|
|
24
|
-
:key="provider"
|
|
25
|
-
@click="login(provider, loginOptions)"
|
|
26
|
-
>
|
|
27
|
-
<WIcon
|
|
28
|
-
v-if="styles[provider]?.logo"
|
|
29
|
-
class="text-xl"
|
|
30
|
-
>
|
|
31
|
-
<component
|
|
32
|
-
:is="styles[provider]?.logo"
|
|
33
|
-
/>
|
|
34
|
-
</WIcon>
|
|
35
|
-
<div>
|
|
36
|
-
Sign in / Register with {{ styles[provider]?.name ?? provider }}
|
|
37
|
-
</div>
|
|
38
|
-
</WButton>
|
|
8
|
+
<template #extra="slotProps">
|
|
9
|
+
<slot
|
|
10
|
+
name="extra"
|
|
11
|
+
v-bind="slotProps"
|
|
12
|
+
/>
|
|
39
13
|
</template>
|
|
40
|
-
|
|
41
|
-
name="extra"
|
|
42
|
-
icon-class="text-xl"
|
|
43
|
-
class="text-l p-2 px-4 [&_label]:justify-start [&_label]:gap-4"
|
|
44
|
-
/>
|
|
45
|
-
</div>
|
|
14
|
+
</WAuth>
|
|
46
15
|
</template>
|
|
47
16
|
|
|
17
|
+
<script>
|
|
18
|
+
export default {};
|
|
19
|
+
</script>
|
|
20
|
+
|
|
48
21
|
<script setup>
|
|
49
|
-
import
|
|
50
|
-
import { twMerge } from "@witchcraft/ui/utils/twMerge";
|
|
51
|
-
import defu from "defu";
|
|
22
|
+
import WAuth from "@witchcraft/ui/components/WAuth";
|
|
52
23
|
import { useRuntimeConfig } from "#app";
|
|
53
|
-
import {
|
|
24
|
+
import { useAttrs } from "#imports";
|
|
54
25
|
import { useAuth } from "../composables/useAuth.js";
|
|
55
|
-
import {
|
|
26
|
+
if (import.meta.dev) {
|
|
27
|
+
console.warn("[nuxt-auth] <LoginProviderButtons> is deprecated and will be removed in a future version.");
|
|
28
|
+
}
|
|
29
|
+
defineOptions({
|
|
30
|
+
inheritAttrs: false
|
|
31
|
+
});
|
|
56
32
|
const rc = useRuntimeConfig();
|
|
57
33
|
const config = rc.public.auth;
|
|
58
34
|
const enabledProviders = config.enabledProviders;
|
|
59
35
|
const $attrs = useAttrs();
|
|
60
|
-
const { darkMode: isDark } = useInjectedDarkMode();
|
|
61
36
|
const props = defineProps({
|
|
62
37
|
providerStyles: { type: Object, required: false, default: () => ({}) },
|
|
63
38
|
useAuthOptions: { type: Object, required: false, default: () => ({}) },
|
|
64
39
|
loginOptions: { type: null, required: false, default: () => ({}) }
|
|
65
40
|
});
|
|
66
41
|
const { login } = useAuth(props.useAuthOptions);
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
style: value?.style ? {
|
|
71
|
-
backgroundColor: (isDark.value ? value.style.bgDark : value.style.bg) ?? "",
|
|
72
|
-
color: (isDark.value ? value.style.textDark : value.style.text) ?? ""
|
|
73
|
-
} : {}
|
|
74
|
-
}])));
|
|
42
|
+
function onLogin(provider) {
|
|
43
|
+
login(provider, props.loginOptions);
|
|
44
|
+
}
|
|
75
45
|
</script>
|
|
@@ -1,27 +1,30 @@
|
|
|
1
1
|
import { useAuth } from "../composables/useAuth.js";
|
|
2
2
|
import type { ProviderNames, ProviderStyle, UseAuthComposableOptions } from "../types.js";
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* @deprecated This component is deprecated and will be removed in a future version.
|
|
5
|
+
*/
|
|
6
|
+
declare const _default: typeof __VLS_export;
|
|
7
|
+
export default _default;
|
|
8
|
+
declare const __VLS_export: __VLS_WithSlots<import("vue").DefineComponent<{
|
|
4
9
|
providerStyles?: Record<ProviderNames, Partial<ProviderStyle>>;
|
|
5
10
|
useAuthOptions?: UseAuthComposableOptions;
|
|
6
11
|
loginOptions?: Parameters<ReturnType<typeof useAuth>["login"]>[1];
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
type __VLS_Slots = {} & {
|
|
13
|
-
extra?: (props: typeof __VLS_20) => any;
|
|
14
|
-
};
|
|
15
|
-
declare const __VLS_base: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {
|
|
12
|
+
}, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<{
|
|
13
|
+
providerStyles?: Record<ProviderNames, Partial<ProviderStyle>>;
|
|
14
|
+
useAuthOptions?: UseAuthComposableOptions;
|
|
15
|
+
loginOptions?: Parameters<ReturnType<typeof useAuth>["login"]>[1];
|
|
16
|
+
}> & Readonly<{}>, {
|
|
16
17
|
providerStyles: Record<ProviderNames, Partial<ProviderStyle>>;
|
|
17
18
|
useAuthOptions: UseAuthComposableOptions;
|
|
18
19
|
loginOptions: {
|
|
19
20
|
devBypass?: boolean;
|
|
20
21
|
};
|
|
21
|
-
}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>, {
|
|
23
|
+
extra?: (props: {
|
|
24
|
+
class: string;
|
|
25
|
+
iconClass: string;
|
|
26
|
+
}) => any;
|
|
27
|
+
}>;
|
|
25
28
|
type __VLS_WithSlots<T, S> = T & {
|
|
26
29
|
new (): {
|
|
27
30
|
$slots: S;
|
|
@@ -18,22 +18,6 @@
|
|
|
18
18
|
</template>
|
|
19
19
|
|
|
20
20
|
<script setup>
|
|
21
|
-
import {
|
|
22
|
-
|
|
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
|
-
}
|
|
21
|
+
import { useAuthMocker } from "../composables/useAuthMocker.js";
|
|
22
|
+
const { provider, username, bypassRegistration, submit } = useAuthMocker();
|
|
39
23
|
</script>
|
|
@@ -1,44 +1,20 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
/** 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. */
|
|
6
|
-
getValidUsernameRoute?: (username: string) => string;
|
|
7
|
-
onSubmitRegistration?: (event: Event, username: string, error: Ref<string>, redirectUrl: string, deeplink?: string) => void;
|
|
8
|
-
id?: string;
|
|
9
|
-
/**
|
|
10
|
-
* When a validator is provided, if it errors, the server is not queried so as to avoid invalid queries. Any errors it produces are also shown.
|
|
11
|
-
*
|
|
12
|
-
* @default defaultZodUsernameSchema
|
|
13
|
-
*/
|
|
14
|
-
usernameSchema?: z.ZodType<string, any, any>;
|
|
15
|
-
};
|
|
16
|
-
declare var __VLS_12: {}, __VLS_19: {}, __VLS_26: {}, __VLS_33: {}, __VLS_41: {
|
|
17
|
-
error: import("nuxt/app").NuxtError<unknown> | undefined;
|
|
18
|
-
registrationError: string;
|
|
19
|
-
localUsernameError: string | undefined;
|
|
20
|
-
id: string;
|
|
21
|
-
};
|
|
22
|
-
type __VLS_Slots = {} & {
|
|
23
|
-
'username-icon-loading'?: (props: typeof __VLS_12) => any;
|
|
24
|
-
} & {
|
|
25
|
-
'username-icon-valid'?: (props: typeof __VLS_19) => any;
|
|
26
|
-
} & {
|
|
27
|
-
'username-icon-invalid'?: (props: typeof __VLS_26) => any;
|
|
28
|
-
} & {
|
|
29
|
-
default?: (props: typeof __VLS_33) => any;
|
|
30
|
-
} & {
|
|
31
|
-
error?: (props: typeof __VLS_41) => any;
|
|
32
|
-
};
|
|
33
|
-
declare const __VLS_base: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {
|
|
34
|
-
debounce: number;
|
|
35
|
-
getValidUsernameRoute: (username: string) => string;
|
|
36
|
-
onSubmitRegistration: (event: Event, username: string, error: Ref<string>, redirectUrl: string, deeplink?: string) => void;
|
|
37
|
-
usernameSchema: z.ZodType<string, any, any>;
|
|
38
|
-
}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
|
|
39
|
-
declare const __VLS_export: __VLS_WithSlots<typeof __VLS_base, __VLS_Slots>;
|
|
1
|
+
import type { StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
|
+
/**
|
|
3
|
+
* @deprecated This component is deprecated and will be removed in a future version.
|
|
4
|
+
*/
|
|
40
5
|
declare const _default: typeof __VLS_export;
|
|
41
6
|
export default _default;
|
|
7
|
+
declare const __VLS_export: __VLS_WithSlots<import("vue").DefineComponent<{
|
|
8
|
+
debounce?: number;
|
|
9
|
+
usernameSchema?: StandardSchemaV1<string, string>;
|
|
10
|
+
id?: string;
|
|
11
|
+
}, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<{
|
|
12
|
+
debounce?: number;
|
|
13
|
+
usernameSchema?: StandardSchemaV1<string, string>;
|
|
14
|
+
id?: string;
|
|
15
|
+
}> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>, {
|
|
16
|
+
default?: (props: {}) => any;
|
|
17
|
+
}>;
|
|
42
18
|
type __VLS_WithSlots<T, S> = T & {
|
|
43
19
|
new (): {
|
|
44
20
|
$slots: S;
|
|
@@ -19,166 +19,62 @@
|
|
|
19
19
|
>
|
|
20
20
|
Username
|
|
21
21
|
</label>
|
|
22
|
-
<
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
:aria-invalid="!!anyError || !isValidUsername && username !== ''"
|
|
32
|
-
v-model="username"
|
|
33
|
-
/>
|
|
34
|
-
<div
|
|
35
|
-
class="absolute top-0 bottom-0 pr-1 right-0"
|
|
36
|
-
aria-live="polite"
|
|
37
|
-
>
|
|
38
|
-
<WIcon class="w-[1rem] pointer-events-none mt-px">
|
|
39
|
-
<slot
|
|
40
|
-
v-if="isLoading"
|
|
41
|
-
name="username-icon-loading"
|
|
42
|
-
>
|
|
43
|
-
<IconSpinner
|
|
44
|
-
class="animate-spin text-neutral-500"
|
|
45
|
-
/>
|
|
46
|
-
<span class="sr-only">Checking availability...</span>
|
|
47
|
-
</slot>
|
|
48
|
-
<slot
|
|
49
|
-
v-else-if="isValidUsername"
|
|
50
|
-
name="username-icon-valid"
|
|
51
|
-
>
|
|
52
|
-
<IconCheck
|
|
53
|
-
class="text-green-500 scale-110"
|
|
54
|
-
/>
|
|
55
|
-
<span class="sr-only">Username available.</span>
|
|
56
|
-
</slot>
|
|
57
|
-
<slot
|
|
58
|
-
v-else-if="anyError || !isValidUsername && username !== ''"
|
|
59
|
-
name="username-icon-invalid"
|
|
60
|
-
>
|
|
61
|
-
<IconInvalid
|
|
62
|
-
class="text-red-500"
|
|
63
|
-
/>
|
|
64
|
-
<span class="sr-only">Username unavailable.</span>
|
|
65
|
-
</slot>
|
|
66
|
-
</WIcon>
|
|
67
|
-
</div>
|
|
68
|
-
</div>
|
|
69
|
-
</div>
|
|
22
|
+
<WAsyncValidatedInput
|
|
23
|
+
:id="usernameId"
|
|
24
|
+
:errors="isDebouncing || username === '' ? [] : errors"
|
|
25
|
+
:status="status"
|
|
26
|
+
:status-text="statusText"
|
|
27
|
+
:can-submit="canSubmit"
|
|
28
|
+
:input-valid="isDebouncing || inputValid"
|
|
29
|
+
v-model="username"
|
|
30
|
+
/>
|
|
70
31
|
</div>
|
|
71
32
|
<slot/>
|
|
72
33
|
<WButton
|
|
73
34
|
:id="submitId"
|
|
74
|
-
:disabled="
|
|
35
|
+
:disabled="!canSubmit"
|
|
75
36
|
class="w-full"
|
|
76
37
|
type="submit"
|
|
77
38
|
>
|
|
78
39
|
Register
|
|
79
40
|
</WButton>
|
|
80
|
-
<slot
|
|
81
|
-
v-if="anyError"
|
|
82
|
-
name="error"
|
|
83
|
-
v-bind="{ error, registrationError, localUsernameError, id: `${usernameId}-error` }"
|
|
84
|
-
>
|
|
85
|
-
<div
|
|
86
|
-
:id="`${usernameId}-error`"
|
|
87
|
-
class="
|
|
88
|
-
border
|
|
89
|
-
border-red-500
|
|
90
|
-
rounded-md
|
|
91
|
-
p-2
|
|
92
|
-
text-red-500
|
|
93
|
-
bg-red-100
|
|
94
|
-
dark:bg-red-950/50
|
|
95
|
-
whitespace-pre-wrap
|
|
96
|
-
break-all
|
|
97
|
-
"
|
|
98
|
-
>
|
|
99
|
-
{{ localUsernameError || registrationError || error }}
|
|
100
|
-
</div>
|
|
101
|
-
</slot>
|
|
102
41
|
</form>
|
|
103
42
|
</template>
|
|
104
43
|
|
|
44
|
+
<script>
|
|
45
|
+
export default {};
|
|
46
|
+
</script>
|
|
47
|
+
|
|
105
48
|
<script setup>
|
|
106
|
-
import {
|
|
107
|
-
import
|
|
108
|
-
import {
|
|
109
|
-
import {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
import IconInvalid from "~icons/lucide/x";
|
|
113
|
-
import { useAuth } from "../composables/useAuth.js";
|
|
114
|
-
import { AUTH_ERROR, defaultZodUsernameSchema } from "../types.js";
|
|
115
|
-
import { getAuthApiRoute } from "../utils/getAuthApiRoute.js";
|
|
116
|
-
const query = useRoute().query;
|
|
117
|
-
const submitId = useId();
|
|
49
|
+
import { WAsyncValidatedInput } from "@witchcraft/ui/components";
|
|
50
|
+
import { useId } from "#imports";
|
|
51
|
+
import { useAuthUserRegistration } from "../composables/useAuthUserRegistration.js";
|
|
52
|
+
if (import.meta.dev) {
|
|
53
|
+
console.warn("[nuxt-auth] <UserRegistration> is deprecated and will be removed in a future version.");
|
|
54
|
+
}
|
|
118
55
|
const props = defineProps({
|
|
119
|
-
debounce: { type: Number, required: false
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
if (!res.success) throw new Error(res.error.format()._errors.join("\n"));
|
|
123
|
-
return getAuthApiRoute(useRuntimeConfig().public, "usernameValid", { username: username2 });
|
|
124
|
-
} },
|
|
125
|
-
onSubmitRegistration: { type: Function, required: false, default: async (_event, username2, error2, redirectUrl, deeplink2) => {
|
|
126
|
-
const res = await $fetch(`${getAuthApiRoute(useRuntimeConfig().public, "register")}${deeplink2 ? `?deeplink=${deeplink2}` : ""}`, {
|
|
127
|
-
cache: "no-store",
|
|
128
|
-
method: "post",
|
|
129
|
-
body: {
|
|
130
|
-
username: username2
|
|
131
|
-
}
|
|
132
|
-
}).catch(async (e) => {
|
|
133
|
-
error2.value = `Registration Error: ${e.data.message}`;
|
|
134
|
-
if (e?.data?.code === AUTH_ERROR.USER_ALREADY_REGISTERED) {
|
|
135
|
-
useAuth().setFetchUserOnNavigation(true);
|
|
136
|
-
await navigateTo(redirectUrl, { external: true });
|
|
137
|
-
}
|
|
138
|
-
});
|
|
139
|
-
if (typeof res === "object" && res.redirectUrl) {
|
|
140
|
-
await navigateTo(res.redirectUrl, { external: true });
|
|
141
|
-
}
|
|
142
|
-
} },
|
|
143
|
-
id: { type: String, required: false },
|
|
144
|
-
usernameSchema: { type: null, required: false, default: defaultZodUsernameSchema }
|
|
56
|
+
debounce: { type: Number, required: false },
|
|
57
|
+
usernameSchema: { type: Object, required: false },
|
|
58
|
+
id: { type: String, required: false }
|
|
145
59
|
});
|
|
146
60
|
const usernameId = props.id ?? useId();
|
|
147
|
-
const
|
|
148
|
-
const
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
default: () => false
|
|
61
|
+
const submitId = useId();
|
|
62
|
+
const {
|
|
63
|
+
username,
|
|
64
|
+
errors,
|
|
65
|
+
status,
|
|
66
|
+
statusText,
|
|
67
|
+
canSubmit,
|
|
68
|
+
handleSubmit,
|
|
69
|
+
inputValid,
|
|
70
|
+
isDebouncing
|
|
71
|
+
} = useAuthUserRegistration({
|
|
72
|
+
debounce: props.debounce,
|
|
73
|
+
usernameSchema: props.usernameSchema,
|
|
74
|
+
statusTextMap: {
|
|
75
|
+
loading: "Checking availability...",
|
|
76
|
+
valid: "Username available.",
|
|
77
|
+
invalid: "Username unavailable."
|
|
165
78
|
}
|
|
166
|
-
);
|
|
167
|
-
const isLoading = computed(() => {
|
|
168
|
-
if (localUsernameError.value) return false;
|
|
169
|
-
return username.value !== "" && (status.value === "pending" || username.value !== debouncedUsername.value);
|
|
170
79
|
});
|
|
171
|
-
const anyError = computed(() => localUsernameError.value || error.value || registrationError.value);
|
|
172
|
-
const deeplink = typeof query.deeplink === "string" ? query.deeplink : void 0;
|
|
173
|
-
const redirect = deeplink ? useRuntimeConfig().public.auth.authRoutes.externalCode : useRuntimeConfig().public.auth.authRoutes.register;
|
|
174
|
-
if (!redirect) throw new Error("No externalCode or register route defined.");
|
|
175
|
-
function handleSubmit(event) {
|
|
176
|
-
props.onSubmitRegistration(
|
|
177
|
-
event,
|
|
178
|
-
username.value,
|
|
179
|
-
registrationError,
|
|
180
|
-
redirect,
|
|
181
|
-
deeplink
|
|
182
|
-
);
|
|
183
|
-
}
|
|
184
80
|
</script>
|
|
@@ -1,44 +1,20 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
/** 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. */
|
|
6
|
-
getValidUsernameRoute?: (username: string) => string;
|
|
7
|
-
onSubmitRegistration?: (event: Event, username: string, error: Ref<string>, redirectUrl: string, deeplink?: string) => void;
|
|
8
|
-
id?: string;
|
|
9
|
-
/**
|
|
10
|
-
* When a validator is provided, if it errors, the server is not queried so as to avoid invalid queries. Any errors it produces are also shown.
|
|
11
|
-
*
|
|
12
|
-
* @default defaultZodUsernameSchema
|
|
13
|
-
*/
|
|
14
|
-
usernameSchema?: z.ZodType<string, any, any>;
|
|
15
|
-
};
|
|
16
|
-
declare var __VLS_12: {}, __VLS_19: {}, __VLS_26: {}, __VLS_33: {}, __VLS_41: {
|
|
17
|
-
error: import("nuxt/app").NuxtError<unknown> | undefined;
|
|
18
|
-
registrationError: string;
|
|
19
|
-
localUsernameError: string | undefined;
|
|
20
|
-
id: string;
|
|
21
|
-
};
|
|
22
|
-
type __VLS_Slots = {} & {
|
|
23
|
-
'username-icon-loading'?: (props: typeof __VLS_12) => any;
|
|
24
|
-
} & {
|
|
25
|
-
'username-icon-valid'?: (props: typeof __VLS_19) => any;
|
|
26
|
-
} & {
|
|
27
|
-
'username-icon-invalid'?: (props: typeof __VLS_26) => any;
|
|
28
|
-
} & {
|
|
29
|
-
default?: (props: typeof __VLS_33) => any;
|
|
30
|
-
} & {
|
|
31
|
-
error?: (props: typeof __VLS_41) => any;
|
|
32
|
-
};
|
|
33
|
-
declare const __VLS_base: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {
|
|
34
|
-
debounce: number;
|
|
35
|
-
getValidUsernameRoute: (username: string) => string;
|
|
36
|
-
onSubmitRegistration: (event: Event, username: string, error: Ref<string>, redirectUrl: string, deeplink?: string) => void;
|
|
37
|
-
usernameSchema: z.ZodType<string, any, any>;
|
|
38
|
-
}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
|
|
39
|
-
declare const __VLS_export: __VLS_WithSlots<typeof __VLS_base, __VLS_Slots>;
|
|
1
|
+
import type { StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
|
+
/**
|
|
3
|
+
* @deprecated This component is deprecated and will be removed in a future version.
|
|
4
|
+
*/
|
|
40
5
|
declare const _default: typeof __VLS_export;
|
|
41
6
|
export default _default;
|
|
7
|
+
declare const __VLS_export: __VLS_WithSlots<import("vue").DefineComponent<{
|
|
8
|
+
debounce?: number;
|
|
9
|
+
usernameSchema?: StandardSchemaV1<string, string>;
|
|
10
|
+
id?: string;
|
|
11
|
+
}, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<{
|
|
12
|
+
debounce?: number;
|
|
13
|
+
usernameSchema?: StandardSchemaV1<string, string>;
|
|
14
|
+
id?: string;
|
|
15
|
+
}> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>, {
|
|
16
|
+
default?: (props: {}) => any;
|
|
17
|
+
}>;
|
|
42
18
|
type __VLS_WithSlots<T, S> = T & {
|
|
43
19
|
new (): {
|
|
44
20
|
$slots: S;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Ref } from "vue";
|
|
2
|
+
export type ExternalCallbackOptions = {
|
|
3
|
+
/** Called with the access token to create a session. */
|
|
4
|
+
saveSession: (accessToken: string) => Promise<void>;
|
|
5
|
+
/** Path to navigate to on success. Defaults to `postRegisteredLogin` from runtime config. */
|
|
6
|
+
successPath?: string;
|
|
7
|
+
/** Path to navigate to on cancel. Defaults to `login` from runtime config. */
|
|
8
|
+
cancelPath?: string;
|
|
9
|
+
};
|
|
10
|
+
export declare function useAuthExternalCallback(opts: ExternalCallbackOptions): {
|
|
11
|
+
authUri: string | undefined;
|
|
12
|
+
initialAccessToken: string | undefined;
|
|
13
|
+
manualAccessToken: Ref<string, string>;
|
|
14
|
+
error: Ref<string | undefined, string | undefined>;
|
|
15
|
+
authorize: (accessToken?: string, isInitial?: boolean) => Promise<void>;
|
|
16
|
+
cancel: () => Promise<void>;
|
|
17
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { ref } from "vue";
|
|
2
|
+
import z from "zod";
|
|
3
|
+
import {
|
|
4
|
+
navigateTo,
|
|
5
|
+
useRoute,
|
|
6
|
+
useRuntimeConfig
|
|
7
|
+
} from "#app";
|
|
8
|
+
import { zExternalCallbackPageQuery } from "../types.js";
|
|
9
|
+
export function useAuthExternalCallback(opts) {
|
|
10
|
+
const rc = useRuntimeConfig();
|
|
11
|
+
const query = useRoute().query;
|
|
12
|
+
const parsedQuery = zExternalCallbackPageQuery.safeParse(query);
|
|
13
|
+
if (parsedQuery.error) throw new Error(z.prettifyError(parsedQuery.error));
|
|
14
|
+
const initialAccessToken = "accessToken" in parsedQuery.data ? parsedQuery.data.accessToken : void 0;
|
|
15
|
+
const authUri = "authUri" in parsedQuery.data ? parsedQuery.data.authUri : void 0;
|
|
16
|
+
if (initialAccessToken) {
|
|
17
|
+
void authorize(initialAccessToken, true);
|
|
18
|
+
}
|
|
19
|
+
const manualAccessToken = ref("");
|
|
20
|
+
const error = ref(void 0);
|
|
21
|
+
async function authorize(accessToken, isInitial = false) {
|
|
22
|
+
if (!accessToken || accessToken.length === 0) {
|
|
23
|
+
if (!isInitial) {
|
|
24
|
+
error.value = "No code provided.";
|
|
25
|
+
}
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const res = await opts.saveSession(accessToken).catch((err) => {
|
|
29
|
+
error.value = err.message;
|
|
30
|
+
return err;
|
|
31
|
+
});
|
|
32
|
+
if (!(res instanceof Error)) {
|
|
33
|
+
await navigateTo(opts.successPath ?? rc.public.auth.authRoutes.postRegisteredLogin);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
async function cancel() {
|
|
37
|
+
await navigateTo(opts.cancelPath ?? rc.public.auth.authRoutes.login);
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
authUri,
|
|
41
|
+
initialAccessToken,
|
|
42
|
+
manualAccessToken,
|
|
43
|
+
error,
|
|
44
|
+
authorize,
|
|
45
|
+
cancel
|
|
46
|
+
};
|
|
47
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { copyToClipboard } from "@alanscodelog/utils/copyToClipboard";
|
|
2
|
+
import { unreachable } from "@alanscodelog/utils/unreachable";
|
|
3
|
+
import { onMounted } from "vue";
|
|
4
|
+
import {
|
|
5
|
+
navigateTo,
|
|
6
|
+
useRoute
|
|
7
|
+
} from "#app";
|
|
8
|
+
import { zUriComponentCodec } from "../types.js";
|
|
9
|
+
export function useAuthExternalCode() {
|
|
10
|
+
const query = useRoute().query;
|
|
11
|
+
const accessToken = query.accessToken;
|
|
12
|
+
if (typeof accessToken !== "string") {
|
|
13
|
+
unreachable("No session_token.");
|
|
14
|
+
}
|
|
15
|
+
const deepLinkUrl = zUriComponentCodec.parse(query.deeplinkUri);
|
|
16
|
+
function copyToken() {
|
|
17
|
+
copyToClipboard(accessToken);
|
|
18
|
+
}
|
|
19
|
+
onMounted(() => {
|
|
20
|
+
if (accessToken) {
|
|
21
|
+
void navigateTo(deepLinkUrl, { external: true });
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
return {
|
|
25
|
+
accessToken,
|
|
26
|
+
deepLinkUrl,
|
|
27
|
+
copyToken
|
|
28
|
+
};
|
|
29
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { ref } from "vue";
|
|
2
|
+
import {
|
|
3
|
+
navigateTo,
|
|
4
|
+
useRoute,
|
|
5
|
+
useRuntimeConfig
|
|
6
|
+
} from "#imports";
|
|
7
|
+
import { getAuthApiRoute } from "../utils/getAuthApiRoute.js";
|
|
8
|
+
export function useAuthMocker() {
|
|
9
|
+
const route = useRoute();
|
|
10
|
+
const username = ref("");
|
|
11
|
+
const bypassRegistration = ref(false);
|
|
12
|
+
const provider = route.query.provider;
|
|
13
|
+
if (!provider || typeof provider !== "string") {
|
|
14
|
+
throw new Error("Missing provider query param.");
|
|
15
|
+
}
|
|
16
|
+
async function submit() {
|
|
17
|
+
const query = route.query;
|
|
18
|
+
delete query.provider;
|
|
19
|
+
const callbackRoute = getAuthApiRoute(useRuntimeConfig().public, "callback", { provider }, {
|
|
20
|
+
devBypass: true,
|
|
21
|
+
username: username.value,
|
|
22
|
+
devBypassRegistration: bypassRegistration.value,
|
|
23
|
+
...query
|
|
24
|
+
});
|
|
25
|
+
await navigateTo(callbackRoute, { external: true });
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
provider,
|
|
29
|
+
username,
|
|
30
|
+
bypassRegistration,
|
|
31
|
+
submit
|
|
32
|
+
};
|
|
33
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
|
+
import type { NitroFetchOptions, NitroFetchRequest } from "nitropack";
|
|
3
|
+
/**
|
|
4
|
+
* Wraps all the async username validation and registration logic.
|
|
5
|
+
*
|
|
6
|
+
* Uses {@link useAsyncValidation} from `@witchcraft/nuxt-utils` under the hood.
|
|
7
|
+
*
|
|
8
|
+
* Notes:
|
|
9
|
+
* - Remember to gate the button with `v-if` canSubmit.
|
|
10
|
+
* - If wrapping handleSubmit, also remember to check `canSubmit` first. Prefer using modifyFetch if you need to add something to the request.
|
|
11
|
+
* - inputValid is only a visual indicator and is valid for empty inputs and when the server is validating a request. Username might still be invalid.
|
|
12
|
+
*/
|
|
13
|
+
export declare function useAuthUserRegistration({ modifyFetch, debounce, statusTextMap, usernameSchema }?: {
|
|
14
|
+
modifyFetch?: (request: {
|
|
15
|
+
cache: "no-store";
|
|
16
|
+
method: "post";
|
|
17
|
+
body: {
|
|
18
|
+
username: string;
|
|
19
|
+
};
|
|
20
|
+
}) => NitroFetchOptions<NitroFetchRequest, any>;
|
|
21
|
+
debounce?: number;
|
|
22
|
+
usernameSchema?: StandardSchemaV1<string, string>;
|
|
23
|
+
statusTextMap?: {
|
|
24
|
+
loading: string;
|
|
25
|
+
valid: string;
|
|
26
|
+
invalid: string;
|
|
27
|
+
};
|
|
28
|
+
}): {
|
|
29
|
+
username: import("vue").Ref<string, string>;
|
|
30
|
+
errors: import("vue").ComputedRef<string[]>;
|
|
31
|
+
status: import("vue").ComputedRef<"loading" | "valid" | "invalid" | undefined>;
|
|
32
|
+
statusText: import("vue").ComputedRef<string>;
|
|
33
|
+
canSubmit: import("vue").ComputedRef<boolean>;
|
|
34
|
+
handleSubmit: (_event: Event) => Promise<void>;
|
|
35
|
+
inputValid: import("vue").ComputedRef<boolean>;
|
|
36
|
+
isDebouncing: import("vue").ComputedRef<boolean>;
|
|
37
|
+
_internal: {
|
|
38
|
+
requestStatus: import("vue").Ref<import("nuxt/app").AsyncDataRequestStatus, import("nuxt/app").AsyncDataRequestStatus>;
|
|
39
|
+
serverError: import("vue").Ref<import("nuxt/app").NuxtError<unknown> | undefined, import("nuxt/app").NuxtError<unknown> | undefined>;
|
|
40
|
+
isLoading: import("vue").ComputedRef<boolean>;
|
|
41
|
+
isServerValid: import("vue").Ref<boolean, boolean>;
|
|
42
|
+
};
|
|
43
|
+
};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { refDebounced } from "@vueuse/core";
|
|
2
|
+
import { useAsyncValidation } from "@witchcraft/nuxt-utils/composables/useAsyncValidation";
|
|
3
|
+
import { computed, ref } from "vue";
|
|
4
|
+
import { navigateTo, useRoute, useRuntimeConfig } from "#app";
|
|
5
|
+
import { useAuth } from "../composables/useAuth.js";
|
|
6
|
+
import { AUTH_ERROR, defaultZodUsernameSchema } from "../types.js";
|
|
7
|
+
import { getAuthApiRoute } from "../utils/getAuthApiRoute.js";
|
|
8
|
+
export function useAuthUserRegistration({
|
|
9
|
+
modifyFetch,
|
|
10
|
+
debounce = 1e3,
|
|
11
|
+
statusTextMap = {
|
|
12
|
+
loading: "Checking availability...",
|
|
13
|
+
valid: "Username available.",
|
|
14
|
+
invalid: "Username unavailable."
|
|
15
|
+
},
|
|
16
|
+
usernameSchema = defaultZodUsernameSchema
|
|
17
|
+
} = {}) {
|
|
18
|
+
const config = useRuntimeConfig().public;
|
|
19
|
+
const query = useRoute().query;
|
|
20
|
+
const username = ref("");
|
|
21
|
+
const debouncedUsername = refDebounced(username, debounce);
|
|
22
|
+
const submitErrors = ref([]);
|
|
23
|
+
const {
|
|
24
|
+
errors,
|
|
25
|
+
status,
|
|
26
|
+
statusText,
|
|
27
|
+
canSubmit,
|
|
28
|
+
inputValid,
|
|
29
|
+
_internal
|
|
30
|
+
} = useAsyncValidation(
|
|
31
|
+
debouncedUsername,
|
|
32
|
+
"auth:username:valid",
|
|
33
|
+
getAuthApiRoute(
|
|
34
|
+
config,
|
|
35
|
+
"usernameValid"
|
|
36
|
+
),
|
|
37
|
+
submitErrors,
|
|
38
|
+
{
|
|
39
|
+
debounce: 0,
|
|
40
|
+
// we already debounced
|
|
41
|
+
statusTextMap,
|
|
42
|
+
schema: usernameSchema,
|
|
43
|
+
getFetchOptions: () => ({
|
|
44
|
+
query: {
|
|
45
|
+
username: debouncedUsername.value
|
|
46
|
+
}
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
);
|
|
50
|
+
const deeplink = typeof query.deeplink === "string" ? query.deeplink : void 0;
|
|
51
|
+
const redirectUrl = deeplink ? config.auth.authRoutes.externalCode : config.auth.authRoutes.register;
|
|
52
|
+
if (!redirectUrl) throw new Error("No externalCode or register route defined.");
|
|
53
|
+
async function handleSubmit(_event) {
|
|
54
|
+
if (!canSubmit.value) return;
|
|
55
|
+
let request = {
|
|
56
|
+
cache: "no-store",
|
|
57
|
+
method: "post",
|
|
58
|
+
body: { username: username.value }
|
|
59
|
+
};
|
|
60
|
+
if (modifyFetch) request = modifyFetch(request);
|
|
61
|
+
const res = await $fetch(`${getAuthApiRoute(config, "register")}${deeplink ? `?deeplink=${deeplink}` : ""}`, request).catch(async (e) => {
|
|
62
|
+
submitErrors.value = [`Registration Error: ${e.data.message}`];
|
|
63
|
+
if (e?.data?.code === AUTH_ERROR.USER_ALREADY_REGISTERED) {
|
|
64
|
+
useAuth().setFetchUserOnNavigation(true);
|
|
65
|
+
await navigateTo(redirectUrl, { external: true });
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
if (typeof res === "object" && res.redirectUrl) {
|
|
69
|
+
await navigateTo(res.redirectUrl, { external: true });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const isDebouncing = computed(() => debouncedUsername.value !== username.value);
|
|
73
|
+
return {
|
|
74
|
+
username,
|
|
75
|
+
errors,
|
|
76
|
+
status,
|
|
77
|
+
statusText,
|
|
78
|
+
canSubmit,
|
|
79
|
+
handleSubmit,
|
|
80
|
+
inputValid,
|
|
81
|
+
isDebouncing,
|
|
82
|
+
_internal
|
|
83
|
+
};
|
|
84
|
+
}
|
|
@@ -80,10 +80,5 @@ export default class GithubProvider implements ProviderHandler<"oauth2", "github
|
|
|
80
80
|
export declare const githubProviderStyle: {
|
|
81
81
|
name: string;
|
|
82
82
|
logo: import("vue").FunctionalComponent<import("vue").SVGAttributes, {}, any, {}>;
|
|
83
|
-
|
|
84
|
-
bg: string;
|
|
85
|
-
bgDark: string;
|
|
86
|
-
text: string;
|
|
87
|
-
textDark: string;
|
|
88
|
-
};
|
|
83
|
+
class: string;
|
|
89
84
|
};
|
|
@@ -51,10 +51,5 @@ export default class GithubProvider {
|
|
|
51
51
|
export const githubProviderStyle = {
|
|
52
52
|
name: "Github",
|
|
53
53
|
logo: GithubLogo,
|
|
54
|
-
|
|
55
|
-
bg: "#fff",
|
|
56
|
-
bgDark: "#000",
|
|
57
|
-
text: "#000",
|
|
58
|
-
textDark: "#fff"
|
|
59
|
-
}
|
|
54
|
+
class: "bg-white text-black dark:bg-black dark:text-white"
|
|
60
55
|
};
|
|
@@ -61,10 +61,5 @@ export default class GoogleProvider implements ProviderHandler<"oauth2_pcke", "g
|
|
|
61
61
|
export declare const googleProviderStyle: {
|
|
62
62
|
name: string;
|
|
63
63
|
logo: import("vue").FunctionalComponent<import("vue").SVGAttributes, {}, any, {}>;
|
|
64
|
-
|
|
65
|
-
bg: string;
|
|
66
|
-
bgDark: string;
|
|
67
|
-
text: string;
|
|
68
|
-
textDark: string;
|
|
69
|
-
};
|
|
64
|
+
class: string;
|
|
70
65
|
};
|
|
@@ -45,10 +45,5 @@ export default class GoogleProvider {
|
|
|
45
45
|
export const googleProviderStyle = {
|
|
46
46
|
name: "Google",
|
|
47
47
|
logo: GoogleLogo,
|
|
48
|
-
|
|
49
|
-
bg: "#fff",
|
|
50
|
-
bgDark: "#000",
|
|
51
|
-
text: "#000",
|
|
52
|
-
textDark: "#fff"
|
|
53
|
-
}
|
|
48
|
+
class: "bg-white text-black dark:bg-black dark:text-white"
|
|
54
49
|
};
|
|
@@ -1,5 +1,3 @@
|
|
|
1
1
|
import type { SignOptions } from "jsonwebtoken";
|
|
2
2
|
/** Promisified version of jsonwebtoken.sign. */
|
|
3
|
-
export declare function signJwt(payload: any, secret: string, opts?: Partial<SignOptions
|
|
4
|
-
algorithm: "none";
|
|
5
|
-
}>): Promise<string>;
|
|
3
|
+
export declare function signJwt(payload: any, secret: string, opts?: Partial<SignOptions>): Promise<string>;
|
|
@@ -12,7 +12,7 @@ import type { SessionManager } from "./SessionManager.js";
|
|
|
12
12
|
import type { AuthOptions, AuthSession, AuthUser, BaseProviderAccountInfo, ProviderHandler, ProviderNames } from "../../types";
|
|
13
13
|
export declare const zState: z.ZodObject<{
|
|
14
14
|
oauthState: z.ZodString;
|
|
15
|
-
additionalState: z.ZodAny
|
|
15
|
+
additionalState: z.ZodOptional<z.ZodAny>;
|
|
16
16
|
deeplink: z.ZodOptional<z.ZodString>;
|
|
17
17
|
}, z.core.$strip>;
|
|
18
18
|
type State = z.infer<typeof zState>;
|
|
@@ -32,6 +32,7 @@ export declare class Auth {
|
|
|
32
32
|
/** The deeplink query param, if any. */
|
|
33
33
|
deeplink?: string) => Promise<boolean>;
|
|
34
34
|
eventHandler: EventHandler;
|
|
35
|
+
/** Careful with this, parse any input carefully before returning it. */
|
|
35
36
|
getAdditionalState?: AuthOptions["additionalState"];
|
|
36
37
|
modifyCallbackRedirect?: AuthOptions["modifyCallbackRedirect"];
|
|
37
38
|
devBypassAuth?: AuthOptions["devBypassAuth"];
|
|
@@ -24,7 +24,7 @@ import { signJwt } from "../helpers/signJwt.js";
|
|
|
24
24
|
import { verifyJwt as jwtVerify } from "../helpers/verifyJwt.js";
|
|
25
25
|
export const zState = z.object({
|
|
26
26
|
oauthState: z.string(),
|
|
27
|
-
additionalState: z.any(),
|
|
27
|
+
additionalState: z.any().optional(),
|
|
28
28
|
deeplink: z.string().optional()
|
|
29
29
|
});
|
|
30
30
|
export const oauth2CallbackQuery = z.object({
|
|
@@ -39,6 +39,7 @@ export class Auth {
|
|
|
39
39
|
deeplinkSchemes;
|
|
40
40
|
onRegister;
|
|
41
41
|
eventHandler;
|
|
42
|
+
/** Careful with this, parse any input carefully before returning it. */
|
|
42
43
|
getAdditionalState;
|
|
43
44
|
modifyCallbackRedirect;
|
|
44
45
|
devBypassAuth;
|
|
@@ -66,7 +67,7 @@ export class Auth {
|
|
|
66
67
|
providerCookieOpts;
|
|
67
68
|
constructor(runtimeConfig, db, usersTable, authAccountsTable, sessionManager, router, opts, env, logger) {
|
|
68
69
|
this.rc = runtimeConfig;
|
|
69
|
-
if (!import.meta.dev && !
|
|
70
|
+
if (!import.meta.dev && (!this.rc.authSecret || this.rc.authSecret.length === 0)) {
|
|
70
71
|
throw new Error("FATAL: Missing or empty runtimeConfig.authSecret (i.e. should be set via NUXT_AUTH_SECRET) in production.");
|
|
71
72
|
}
|
|
72
73
|
this.providerCookieOpts = {
|
|
@@ -161,7 +162,15 @@ export class Auth {
|
|
|
161
162
|
return user ?? false;
|
|
162
163
|
}));
|
|
163
164
|
router.get(apiRoutes.externalExchange, defineEventHandler(async (event) => {
|
|
164
|
-
const
|
|
165
|
+
const authorization = getRequestHeaders(event).authorization;
|
|
166
|
+
if (!authorization || !/^Bearer\s+\S+$/.test(authorization)) {
|
|
167
|
+
throw createError({
|
|
168
|
+
status: 400,
|
|
169
|
+
statusMessage: "Invalid Authorization header. Expected 'Bearer <token>'.",
|
|
170
|
+
data: { code: AUTH_ERROR.INVALID_ACCESS_TOKEN }
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
const accessToken = authorization.slice("Bearer ".length);
|
|
165
174
|
const decoded = await this.verifyAccessToken(accessToken).catch((err) => createError({
|
|
166
175
|
status: 400,
|
|
167
176
|
statusMessage: err.message,
|
|
@@ -466,7 +475,7 @@ export class Auth {
|
|
|
466
475
|
});
|
|
467
476
|
throw createError({
|
|
468
477
|
status: 400,
|
|
469
|
-
statusMessage: `Invalid provider: ${provider}
|
|
478
|
+
statusMessage: `Invalid provider: ${provider}`,
|
|
470
479
|
data: { code: AUTH_ERROR.UNKNOWN_PROVIDER }
|
|
471
480
|
});
|
|
472
481
|
}
|
|
@@ -639,7 +648,11 @@ export class Auth {
|
|
|
639
648
|
return `${scheme}${this.rc.public.auth.authRoutes.deeplink}`;
|
|
640
649
|
}
|
|
641
650
|
async verifyAccessToken(accessToken) {
|
|
642
|
-
return jwtVerify(
|
|
651
|
+
return jwtVerify(
|
|
652
|
+
accessToken,
|
|
653
|
+
this.rc.authSecret,
|
|
654
|
+
{ algorithms: ["HS256"] }
|
|
655
|
+
).catch((err) => {
|
|
643
656
|
throw createError({
|
|
644
657
|
status: 400,
|
|
645
658
|
statusMessage: err.message,
|
|
@@ -650,7 +663,9 @@ export class Auth {
|
|
|
650
663
|
async createAccessToken(userId, payload = {}) {
|
|
651
664
|
return signJwt({ ...payload, userId }, this.rc.authSecret, {
|
|
652
665
|
// ms is different package version so type doesn't match
|
|
653
|
-
expiresIn: this.externalAccessTokenExpiresIn
|
|
666
|
+
expiresIn: this.externalAccessTokenExpiresIn,
|
|
667
|
+
// this is the default but just to be explicit
|
|
668
|
+
algorithm: "HS256"
|
|
654
669
|
});
|
|
655
670
|
}
|
|
656
671
|
}
|
|
@@ -19,7 +19,7 @@ export declare class SessionManager {
|
|
|
19
19
|
constructor(rc: RuntimeConfig, db: SessionManager["db"], sessionTable: SessionManager["sessionTable"], userTable: SessionManager["userTable"], options?: DeepPartial<SessionManager["options"]>);
|
|
20
20
|
generateSessionToken(): string;
|
|
21
21
|
createSession<TId = string>(sessionToken: string, userId: TId): Promise<AuthSession>;
|
|
22
|
-
createSessionCookie(
|
|
22
|
+
createSessionCookie(hashedSessionId: string): SessionCookie;
|
|
23
23
|
createBlankSessionCookie(): SessionCookie;
|
|
24
24
|
validateSessionToken(sessionToken: string): Promise<SessionValidationResult>;
|
|
25
25
|
invalidateSession(sessionId: string): Promise<void>;
|
|
@@ -41,10 +41,10 @@ export class SessionManager {
|
|
|
41
41
|
await this.db.insert(this.sessionTable).values(session);
|
|
42
42
|
return session;
|
|
43
43
|
}
|
|
44
|
-
createSessionCookie(
|
|
44
|
+
createSessionCookie(hashedSessionId) {
|
|
45
45
|
return {
|
|
46
46
|
name: this.options.sessionCookie.name,
|
|
47
|
-
value:
|
|
47
|
+
value: hashedSessionId,
|
|
48
48
|
attributes: {
|
|
49
49
|
...this.options.sessionCookie.attributes,
|
|
50
50
|
maxAge: (/* @__PURE__ */ new Date()).getTime() + this.options.expiresAt
|
|
@@ -62,8 +62,8 @@ export class SessionManager {
|
|
|
62
62
|
};
|
|
63
63
|
}
|
|
64
64
|
async validateSessionToken(sessionToken) {
|
|
65
|
-
const
|
|
66
|
-
const result = await this.db.select({ user: this.userTable, session: this.sessionTable }).from(this.sessionTable).innerJoin(this.userTable, eq(this.sessionTable.userId, this.userTable.id)).where(eq(this.sessionTable.id,
|
|
65
|
+
const hashedSessionId = encodeHexLowerCase(sha256(new TextEncoder().encode(sessionToken)));
|
|
66
|
+
const result = await this.db.select({ user: this.userTable, session: this.sessionTable }).from(this.sessionTable).innerJoin(this.userTable, eq(this.sessionTable.userId, this.userTable.id)).where(eq(this.sessionTable.id, hashedSessionId));
|
|
67
67
|
if (result.length < 1) {
|
|
68
68
|
return { session: null, user: null, fresh: false };
|
|
69
69
|
}
|
|
@@ -88,6 +88,7 @@ export class SessionManager {
|
|
|
88
88
|
};
|
|
89
89
|
}
|
|
90
90
|
async invalidateSession(sessionId) {
|
|
91
|
-
|
|
91
|
+
const hashedSessionId = encodeHexLowerCase(sha256(new TextEncoder().encode(sessionId)));
|
|
92
|
+
await this.db.delete(this.sessionTable).where(eq(this.sessionTable.id, hashedSessionId));
|
|
92
93
|
}
|
|
93
94
|
}
|
|
@@ -14,8 +14,8 @@ export function createAuthMiddleware(sessionManager, logger) {
|
|
|
14
14
|
fresh,
|
|
15
15
|
redact: { sessionToken, session }
|
|
16
16
|
});
|
|
17
|
-
if (fresh) {
|
|
18
|
-
const cookie = sessionManager.createSessionCookie(
|
|
17
|
+
if (fresh && session) {
|
|
18
|
+
const cookie = sessionManager.createSessionCookie(sessionToken);
|
|
19
19
|
setCookie(event, cookie.name, cookie.value, cookie.attributes);
|
|
20
20
|
}
|
|
21
21
|
if (!session) {
|
package/dist/runtime/types.d.ts
CHANGED
|
@@ -226,15 +226,9 @@ export interface BaseProviderAccountInfo extends AuthAccount, InitialAccountInfo
|
|
|
226
226
|
export type ProviderStyle = {
|
|
227
227
|
name: string;
|
|
228
228
|
logo: any;
|
|
229
|
-
|
|
230
|
-
bg: string;
|
|
231
|
-
text: string;
|
|
232
|
-
bgDark: string;
|
|
233
|
-
textDark: string;
|
|
234
|
-
};
|
|
229
|
+
class?: string;
|
|
235
230
|
};
|
|
236
231
|
export declare const providerStylesInjectionKey: InjectionKey<Partial<Record<ProviderNames, Partial<ProviderStyle>>>>;
|
|
237
|
-
export type FullProviderStyles = Record<"github" | "google", ProviderStyle> & Record<ProviderNames, Partial<ProviderStyle>>;
|
|
238
232
|
export type UseAuthComposableOptions = {
|
|
239
233
|
/**
|
|
240
234
|
* Handle login and logout actions yourself.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@witchcraft/nuxt-auth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Nuxt module for logging.",
|
|
5
5
|
"repository": "witchcraftjs/nuxt-auth",
|
|
6
6
|
"license": "MIT",
|
|
@@ -33,15 +33,17 @@
|
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"@alanscodelog/tsconfigs": "^6.3.0",
|
|
36
|
-
"@alanscodelog/utils": "^6.
|
|
36
|
+
"@alanscodelog/utils": "^6.3.1",
|
|
37
37
|
"@iconify/json": "^2.2.393",
|
|
38
38
|
"@nuxt/kit": "^4.1.3",
|
|
39
39
|
"@oslojs/crypto": "^1.0.1",
|
|
40
40
|
"@oslojs/encoding": "^1.1.0",
|
|
41
|
+
"@standard-schema/spec": "^1.1.0",
|
|
41
42
|
"@types/jsonwebtoken": "^9.0.10",
|
|
42
43
|
"@vueuse/core": "^13.9.0",
|
|
43
44
|
"@witchcraft/nuxt-logger": "^0.0.11",
|
|
44
|
-
"@witchcraft/
|
|
45
|
+
"@witchcraft/nuxt-utils": "^0.3.13",
|
|
46
|
+
"@witchcraft/ui": "^0.5.3",
|
|
45
47
|
"arctic": "^3.7.0",
|
|
46
48
|
"cookie-es": "^3.1.1",
|
|
47
49
|
"defu": "^6.1.4",
|
|
@@ -75,6 +77,7 @@
|
|
|
75
77
|
"tailwindcss": "^4.1.14",
|
|
76
78
|
"typescript": "^5.9.3",
|
|
77
79
|
"vitest": "^3.2.4",
|
|
80
|
+
"vue": "^3.5.0",
|
|
78
81
|
"vue-tsc": "^3.3.3"
|
|
79
82
|
},
|
|
80
83
|
"release": {
|