1.0.4 • Published 3 years ago

rock-package-sso-teste v1.0.4

Weekly downloads
35
License
ISC
Repository
-
Last release
3 years ago

RockOS SSO

Instalation

npm i git+ssh://git@gitlab.rockcontent.com:2222/rockos/package/rockos-package-sso.git

Usage

import { createSso } from 'rock-logs'

// It can also be 'prd' or 'dev'
// If it's dev, you must pass an URL as the second parameter
// Example: createSso('dev', 'http://localhost:3000')
const sso = createSso('hmg')

sso.addEventListener('login', token => {
  const decoded = jwt.decode(token)
  setName(decoded.name)
})

sso.addEventListener('loginfailure', () => {
  console.log('login failed')
})

sso.login()

Methods

.addEventListener

Add listeners to one of the following events:

  • login – Triggered after the login is succeeded. Returns the generated JWT token
  • loginfailure – Triggered when an error occurs while logging in. Returns the given error object
  • logout – Triggered after the user is successfully logged out
sso.addEventListener('login', token => {
  const decoded = jwt.decode(token)
  setName(decoded.name)
})

sso.addEventListener('loginfailure', () => {
  console.log('login failed')
})


sso.addEventListener('logout', () => {
  console.log('logged out successfully')
})

.getToken

Return the authenticated user token. It's often used to know wheter the current user is logged in or not

const token = await sso.getToken()

console.log(token) // JWT Token

.login

Used to authenticate the current user. It can be used without parameters, which will redirect the user to RockOS Account or passing { email: string, password: string } if the login page is implemented by the consumer.

Both authentication methods will trigger the login or loginfailure events after the login is finished.

// Will redirect to https://account.rockcontent.com
await sso.login()

// Will authenticate the user and trigger the 'login' event when it's ready.
await sso.login({
  email: 'johndoe@rockcontent.com',
  password: 'johndoesecret'
})

.logout

Will logout the user. It involves clean all cookies and invalidate the user token. This will trigger the logout event after the logout is finished.

// Will trigger 'logout' event after it's done
sso.logout()

.loginWithGoogle

Will authenticate the user using a google account. It should receive a redirectUrl in order to redirect the user to the right page after then authentication is complete. This one does not triggers login and loginfailure events

sso.loginWithGoogle()
1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago