feat: basic sdks

This commit is contained in:
janishutz committed 2026-09-10 15:51:01 +02:00
1 parent 4d7b326df5
commit 9652003018
11 files changed
+3866 -12

No files matched your search

View File
Whitespace-only changes.
+7
View File
@@ -0,0 +1,7 @@
#!/bin/bash
tsc --declaration
cp ./package.json ./dist
cp ./README.md ./dist
echo "Done"
+1 -1
View File
@@ -1,5 +1,5 @@
{
"name": "@janishutz/oidc-login-browser",
"name": "@janishutz/oidc-login-sdk-browser",
"version": "1.0.0",
"description": "SDK to communicate with and log into a backend running express-openid-connect",
"homepage": "https://github.com/janishutz/oidc-login-sdk-browser#readme",
+36 -3
View File
@@ -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';
};
+2 -1
View File
@@ -5,8 +5,9 @@ export interface Config {
'defaultAuthErrorResolution': AuthErrorResolution;
'authErrorEvent'?: string;
'authCheckEndpoint'?: string;
'loginEndpoing'?: string;
'loginEndpoint'?: string;
'logoutEndpoint'?: string;
'checkAutoRedirect'?: boolean;
}
declare global {
+9 -6
View File
@@ -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' );