@i-360/auth-ui
v0.1.2
Published
i360 Auth UI
Downloads
5
Readme
i360 Auth UI
Package for authenticating a user using their Koch ID
Setup
- Clone this repo locally =>
git clone [email protected]:i-360/dataops/auth-ui.git
- Install dependencies =>
pnpm install
(from repo root directory) - Run the example app =>
pnpm example
Use
The package is published to the NPM registry and can be installed with your preferred package manager:
- NPM =>
npm i @i-360/auth-ui
- Yarn =>
yarn add @i-360/auth-ui
- PNPM =>
pnpm add @i-360/auth-ui
AuthService
After installing, import the Auth class into the application and initialize it. Then call the login method.
user<Object|undefined>
=> The user object or undefined when not logged in (getter only, can not be set)token<string|undefined>
=> The users token or undefined when not logged in (getter only, can not be set)login<function>
=> Start the login flow for the userlogout<function>
=> Log a user out of the applicationstatus<function>
=> Check the login status of the Userexpired<function>
=> Validate if the user token is expiredonToken<function>
=> Should be called with the idToken from within the pop-window when redirecteddecode<function>
=> Helper for decoding JWT tokens (Not commonly used)
Example
// Initialize the Auth class
const auth = new Auth({
// URL used to login the user
loginUrl: `https://example.i-360.com/kochid/login`,
// URL used to logout the user
logoutUrl: `https://example.i-360.com/kochid/logout`,
// URL to redirect to once authenticated
redirectUri: `my-application.com`,
// Login callback called when the user is logged in
onLogin: (user, token) => { /* Handle user and token */ },
// Error callback called when the there is an error during authentication
onError: (error) => { /* Handle authentication error */ },
// Logout callback called when the user is logged out
onLogout: () => { /* Handle User logout */ },
})
// Call the login method to log the user into the application
// Calls the passed in `onLogin` callback after logging in the user
await auth.login()
// Get the current user or token, both are undefined if not logged in
const user = auth.user
const token = auth.token
// Setting the user or token manually will NOT work. A warning will be printed to the console
auth.user = {}
auth.token = `custom-token`
// Check if the token is expired, returned false if no user or token, or the token is expired
const isExpired = auth.expired()
// Call the logout method to log the user out of the application
// Calls the passed in `onLogout` callback after logging out the user
auth.logout()