mirror of
https://github.com/janishutz/oidc-login-sdk.git
synced 2026-10-08 17:36:20 +02:00
feat: basic sdks
This commit is contained in:
1 parent
4d7b326df5
commit
9652003018
11 files changed
+3866
-12
No files matched your search
+36
-3
@@ -1,8 +1,41 @@
|
||||
import request, {
|
||||
AuthError
|
||||
} from './request.js';
|
||||
import config from './config.js';
|
||||
|
||||
export const login = () => {
|
||||
sessionStorage.setItem( 'redirect', location.pathname );
|
||||
location.href = '/login';
|
||||
location.href = config.get().loginEndpoint ?? '/auth/v2/login';
|
||||
};
|
||||
|
||||
export const check = () => {};
|
||||
export const check = async () => {
|
||||
let status: boolean;
|
||||
|
||||
export const logout = () => {};
|
||||
try {
|
||||
status = ( await request.get( config.get().authCheckEndpoint ?? '/auth/v2/check' ) ).ok;
|
||||
} catch ( e ) {
|
||||
if ( e instanceof AuthError ) {
|
||||
status = false;
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
if ( !status && config.get().checkAutoRedirect ) {
|
||||
location.href = getRedirect();
|
||||
}
|
||||
|
||||
return status;
|
||||
};
|
||||
|
||||
export const getRedirect = (): string | null => {
|
||||
const item = sessionStorage.getItem( 'redirect' );
|
||||
|
||||
sessionStorage.removeItem( 'redirect' );
|
||||
|
||||
return item;
|
||||
};
|
||||
|
||||
export const logout = async () => {
|
||||
location.href = config.get().logoutEndpoint ?? '/auth/v2/logout';
|
||||
};
|
||||
Vendored
+2
-1
@@ -5,8 +5,9 @@ export interface Config {
|
||||
'defaultAuthErrorResolution': AuthErrorResolution;
|
||||
'authErrorEvent'?: string;
|
||||
'authCheckEndpoint'?: string;
|
||||
'loginEndpoing'?: string;
|
||||
'loginEndpoint'?: string;
|
||||
'logoutEndpoint'?: string;
|
||||
'checkAutoRedirect'?: boolean;
|
||||
}
|
||||
|
||||
declare global {
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
import {
|
||||
AuthErrorResolution
|
||||
} from './dtype.js';
|
||||
import config from './config.js';
|
||||
import {
|
||||
login
|
||||
@@ -8,13 +11,13 @@ export class AuthError extends Error {}
|
||||
export class UnownedError extends Error {}
|
||||
|
||||
|
||||
const get = async ( url: string ): Promise<Response> => {
|
||||
const get = async ( url: string, authErrorResolution?: AuthErrorResolution ): Promise<Response> => {
|
||||
return await wrapper( url, {
|
||||
'credentials': 'include'
|
||||
} );
|
||||
}, authErrorResolution );
|
||||
};
|
||||
|
||||
const post = async ( url: string, payload: string, mime: string = 'application/json' ): Promise<Response> => {
|
||||
const post = async ( url: string, payload: string, mime: string = 'application/json', authErrorResolution?: AuthErrorResolution ): Promise<Response> => {
|
||||
return await wrapper( url, {
|
||||
'credentials': 'include',
|
||||
'body': payload,
|
||||
@@ -22,10 +25,10 @@ const post = async ( url: string, payload: string, mime: string = 'application/j
|
||||
'headers': {
|
||||
'Content-Type': mime ?? 'application/json'
|
||||
}
|
||||
} );
|
||||
}, authErrorResolution );
|
||||
};
|
||||
|
||||
const wrapper = async ( url: string, opts: RequestInit ): Promise<Response> => {
|
||||
const wrapper = async ( url: string, opts: RequestInit, authErrorResolution?: AuthErrorResolution ): Promise<Response> => {
|
||||
const res = await fetch( config.get().backendURL + url, {
|
||||
'redirect': 'manual',
|
||||
...opts
|
||||
@@ -36,7 +39,7 @@ const wrapper = async ( url: string, opts: RequestInit ): Promise<Response> => {
|
||||
document.dispatchEvent( new CustomEvent( 'autherror' ) );
|
||||
}
|
||||
|
||||
if ( config.get().defaultAuthErrorResolution === 'resolve' ) {
|
||||
if ( ( authErrorResolution && authErrorResolution === 'resolve' ) || ( !authErrorResolution && config.get().defaultAuthErrorResolution === 'resolve' ) ) {
|
||||
login();
|
||||
} else {
|
||||
throw new AuthError( 'ERR_USER_UNAUTHORIZED' );
|
||||
|
||||
Reference in new issue
Block a user