@thecodeorigin/auth 0.0.4 → 0.0.6
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.json +1 -1
- package/dist/module.mjs +1 -0
- package/dist/runtime/app/composables/useAuth.d.ts +5 -2
- package/dist/runtime/app/composables/useAuth.js +13 -8
- package/dist/runtime/server/api/_auth/sign-out.post.d.ts +4 -0
- package/dist/runtime/server/api/_auth/sign-out.post.js +6 -0
- package/dist/runtime/server/routes/sign-in.get.js +6 -2
- package/dist/runtime/server/routes/sign-out.get.js +3 -26
- package/package.json +1 -1
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -29,6 +29,7 @@ const module$1 = defineNuxtModule({
|
|
|
29
29
|
addServerHandler({ route: routes.signOut, handler: resolver.resolve("./runtime/server/routes/sign-out.get") });
|
|
30
30
|
for (const [route, file] of [
|
|
31
31
|
["/api/_auth/session", "session.get"],
|
|
32
|
+
["/api/_auth/sign-out", "sign-out.post"],
|
|
32
33
|
["/api/_auth/organizations", "organizations.get"],
|
|
33
34
|
["/api/_auth/organizations/switch", "organizations/switch.post"],
|
|
34
35
|
["/api/_auth/impersonatable-users", "impersonatable-users.get"],
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ImpersonationCandidate, RpOrganization } from '../../../contract/index.js';
|
|
2
|
+
export type SignInPrompt = 'login' | 'select_account' | 'consent' | 'none';
|
|
2
3
|
export declare function useAuth(): {
|
|
3
4
|
session: import("vue").Ref<{
|
|
4
5
|
user: {
|
|
@@ -100,7 +101,9 @@ export declare function useAuth(): {
|
|
|
100
101
|
}>;
|
|
101
102
|
impersonate: (userId: string) => Promise<void>;
|
|
102
103
|
stopImpersonating: () => Promise<void>;
|
|
103
|
-
signIn: (redirect?: string
|
|
104
|
-
|
|
104
|
+
signIn: (redirect?: string, options?: {
|
|
105
|
+
prompt?: SignInPrompt;
|
|
106
|
+
}) => string | false | void | import("vue-router").RouteLocationAsRelativeGeneric | import("vue-router").RouteLocationAsPathGeneric | Promise<false | void | import("vue-router").NavigationFailure>;
|
|
107
|
+
signOut: () => Promise<void>;
|
|
105
108
|
refresh: () => Promise<void>;
|
|
106
109
|
};
|
|
@@ -32,14 +32,19 @@ export function useAuth() {
|
|
|
32
32
|
async function stopImpersonating() {
|
|
33
33
|
session.value = await $fetch("/api/_auth/stop-impersonating", { method: "POST" });
|
|
34
34
|
}
|
|
35
|
-
function signIn(redirect) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
)
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
return navigateTo(routes.
|
|
35
|
+
function signIn(redirect, options) {
|
|
36
|
+
const params = new URLSearchParams();
|
|
37
|
+
if (redirect)
|
|
38
|
+
params.set("redirect", redirect);
|
|
39
|
+
if (options?.prompt)
|
|
40
|
+
params.set("prompt", options.prompt);
|
|
41
|
+
const qs = params.toString();
|
|
42
|
+
return navigateTo(qs ? `${routes.signIn}?${qs}` : routes.signIn, { external: true });
|
|
43
|
+
}
|
|
44
|
+
async function signOut() {
|
|
45
|
+
await $fetch("/api/_auth/sign-out", { method: "POST" }).catch(() => {
|
|
46
|
+
});
|
|
47
|
+
session.value = null;
|
|
43
48
|
}
|
|
44
49
|
return {
|
|
45
50
|
session,
|
|
@@ -2,13 +2,16 @@ import { createError, defineEventHandler, getQuery, sendRedirect, setCookie } fr
|
|
|
2
2
|
import { useRuntimeConfig } from "nitropack/runtime";
|
|
3
3
|
import { withQuery } from "ufo";
|
|
4
4
|
import { callbackRedirectUri, pkceChallenge, randomString, safePath } from "../utils/oidc.js";
|
|
5
|
+
const ALLOWED_PROMPTS = /* @__PURE__ */ new Set(["login", "select_account", "consent", "none"]);
|
|
5
6
|
export default defineEventHandler(async (event) => {
|
|
6
7
|
const { auth: runtimeConfig, public: { auth: publicRuntimeConfig } } = useRuntimeConfig();
|
|
7
8
|
if (!publicRuntimeConfig.domain || !publicRuntimeConfig.clientId || !runtimeConfig.clientSecret)
|
|
8
9
|
throw createError({ statusCode: 503, statusMessage: "Auth not configured" });
|
|
10
|
+
const query = getQuery(event);
|
|
9
11
|
const state = randomString(32);
|
|
10
12
|
const verifier = randomString(64);
|
|
11
|
-
const redirectTo = safePath(
|
|
13
|
+
const redirectTo = safePath(query.redirect, publicRuntimeConfig.routes.home);
|
|
14
|
+
const prompt = typeof query.prompt === "string" && ALLOWED_PROMPTS.has(query.prompt) ? query.prompt : void 0;
|
|
12
15
|
const opts = { httpOnly: true, secure: !import.meta.dev, sameSite: "lax", maxAge: 600, path: publicRuntimeConfig.routes.callback };
|
|
13
16
|
setCookie(event, "tco_state", state, opts);
|
|
14
17
|
setCookie(event, "tco_verifier", verifier, opts);
|
|
@@ -20,6 +23,7 @@ export default defineEventHandler(async (event) => {
|
|
|
20
23
|
scope: publicRuntimeConfig.scopes.join(" "),
|
|
21
24
|
state,
|
|
22
25
|
code_challenge: await pkceChallenge(verifier),
|
|
23
|
-
code_challenge_method: "S256"
|
|
26
|
+
code_challenge_method: "S256",
|
|
27
|
+
...prompt ? { prompt } : {}
|
|
24
28
|
}));
|
|
25
29
|
});
|
|
@@ -1,31 +1,8 @@
|
|
|
1
|
-
import { defineEventHandler,
|
|
1
|
+
import { defineEventHandler, sendRedirect } from "h3";
|
|
2
2
|
import { useRuntimeConfig } from "nitropack/runtime";
|
|
3
|
-
import { $fetch } from "ofetch";
|
|
4
|
-
import { withQuery } from "ufo";
|
|
5
3
|
import { destroySession } from "../utils/session.js";
|
|
6
4
|
export default defineEventHandler(async (event) => {
|
|
7
|
-
const {
|
|
8
|
-
|
|
9
|
-
if (rec?.accessToken) {
|
|
10
|
-
await $fetch(`https://${publicRuntimeConfig.domain}/api/auth/oauth2/revoke`, {
|
|
11
|
-
method: "POST",
|
|
12
|
-
body: new URLSearchParams({
|
|
13
|
-
token: rec.refreshToken ?? rec.accessToken,
|
|
14
|
-
token_type_hint: rec.refreshToken ? "refresh_token" : "access_token",
|
|
15
|
-
client_id: publicRuntimeConfig.clientId,
|
|
16
|
-
client_secret: runtimeConfig.clientSecret
|
|
17
|
-
}).toString(),
|
|
18
|
-
headers: { "Content-Type": "application/x-www-form-urlencoded" }
|
|
19
|
-
}).catch(() => {
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
const postLogoutRedirectUri = `${getRequestProtocol(event)}://${getRequestHost(event)}${publicRuntimeConfig.routes.home}`;
|
|
23
|
-
if (rec?.idToken) {
|
|
24
|
-
return sendRedirect(event, withQuery(`https://${publicRuntimeConfig.domain}/api/auth/oauth2/end-session`, {
|
|
25
|
-
id_token_hint: rec.idToken,
|
|
26
|
-
client_id: publicRuntimeConfig.clientId,
|
|
27
|
-
post_logout_redirect_uri: postLogoutRedirectUri
|
|
28
|
-
}));
|
|
29
|
-
}
|
|
5
|
+
const { public: { auth: publicRuntimeConfig } } = useRuntimeConfig();
|
|
6
|
+
await destroySession(event);
|
|
30
7
|
return sendRedirect(event, publicRuntimeConfig.routes.home);
|
|
31
8
|
});
|
package/package.json
CHANGED