Skip to content

Commit cb5ec4c

Browse files
author
Zhazhazhu
committed
feat: add redirectUriKey config
1 parent e5c58f6 commit cb5ec4c

File tree

5 files changed

+18
-11
lines changed

5 files changed

+18
-11
lines changed

example/client/src/oidc.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ function runAuth() {
4141
},
4242
addUserSignedOut: () => {},
4343
},
44+
//your key customization of oidc redirect callback
45+
redirectUriKey: "CUSTOM_REDIRECT_URI",
4446
});
4547
}
4648

src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { UserManager, UserManagerEvents } from "oidc-client-ts";
22
import { unref } from "vue";
3+
import { oidcRedirectUriKey } from "./keys";
34
import { useOidcStore } from "./store";
45
import { inlineOidcEvents } from "./store/events";
56
import { VueOidcSettings } from "./store/index";
@@ -34,12 +35,17 @@ export interface CreateOidcOptions {
3435
* refresh token
3536
*/
3637
refreshToken?: RefreshTokenConfig;
38+
/**
39+
* key of oidc redirect callback
40+
*/
41+
redirectUriKey?: string;
3742
}
3843

3944
export function createOidc(options: CreateOidcOptions) {
4045
const _options = { ...inlineCreateOidcOptions, ...options };
4146
const { oidcSettings, auth, refreshToken } = _options;
4247
const events = { ...inlineOidcEvents, ...options.events };
48+
oidcRedirectUriKey.value = options.redirectUriKey || oidcRedirectUriKey.value;
4349

4450
unref(state).settings = _options;
4551
unref(state).oidcSettings = oidcSettings;

src/keys.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
export const OIDC_REDIRECT_URI = "OIDC_REDIRECT_URI";
1+
import { ref } from "vue";
2+
3+
export const oidcRedirectUriKey = ref("OIDC_REDIRECT_URI");

src/store/index.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { OIDC_REDIRECT_URI } from "@/keys";
1+
import { oidcRedirectUriKey } from "@/keys";
22
import { MaybeNull } from "@/types";
3-
import { useStorage, type RemovableRef } from "@vueuse/core";
3+
import { useStorage } from "@vueuse/core";
44
import {
55
User,
66
UserManager,
@@ -39,11 +39,6 @@ export interface OidcActions {
3939
removeUser(): void;
4040
}
4141

42-
export const storage: RemovableRef<string> = useStorage<string>(
43-
OIDC_REDIRECT_URI,
44-
""
45-
);
46-
4742
const state: UnwrapNestedRefs<OidcState> = reactive<OidcState>({
4843
settings: null,
4944
oidcSettings: null,
@@ -54,7 +49,7 @@ const state: UnwrapNestedRefs<OidcState> = reactive<OidcState>({
5449
hasExpiresAt: computed(
5550
() => Date.now() / 1000 > state.user?.expires_at! || false
5651
),
57-
redirect_uri: storage.value,
52+
redirect_uri: useStorage<string>(oidcRedirectUriKey.value, "").value,
5853
});
5954

6055
const actions: OidcActions = {

src/useAuth.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { toValue } from "@vueuse/core";
1+
import { toValue, useStorage } from "@vueuse/core";
22
import {
33
SigninRedirectArgs,
44
SigninSilentArgs,
55
SignoutRedirectArgs,
66
User,
77
} from "oidc-client-ts";
88
import { unref } from "vue";
9-
import { storage, useOidcStore } from "./store";
9+
import { oidcRedirectUriKey } from "./keys";
10+
import { useOidcStore } from "./store";
1011
import { isPathOfCallback } from "./utils";
1112

1213
const { state, actions } = useOidcStore();
@@ -36,6 +37,7 @@ function signoutRedirect(arg?: SignoutRedirectArgs) {
3637
async function autoAuthenticate(uri: string = "") {
3738
let timer: NodeJS.Timer | null = null;
3839
const user = (await unref(state).userManager?.getUser()) || unref(state).user;
40+
const storage = useStorage<string>(oidcRedirectUriKey.value, "");
3941

4042
//if the user and pathCallback is not, then we can authenticate
4143
if (!user && !isPathOfCallback()) {

0 commit comments

Comments
 (0)