1.1.0 • Published 1 year ago

@rockr/sso v1.1.0

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

RockOS SSO

Instalation

npm i @rockr/sso

Usage

import { createSso } from '@rockr/sso'

// 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

.setToken

This method must be used if you're using SSO only on you back-end. After the token is generated, use .setToken to save the token in the RockOS Cookies

await sso.setToken(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()
2.0.5-alpha1

2 years ago

2.0.3-alpha1

2 years ago

2.0.1-alpha1

2 years ago

2.0.18-alpha1

2 years ago

2.0.29-alpha1

2 years ago

2.0.28-alpha1

2 years ago

2.0.27-alpha1

2 years ago

2.0.26-alpha1

2 years ago

2.0.20-alpha1

2 years ago

2.0.21-alpha1

2 years ago

2.0.25-alpha1

2 years ago

2.0.24-alpha1

2 years ago

2.0.22-alpha1

2 years ago

2.0.23-alpha1

2 years ago

2.0.9-alpha1

2 years ago

2.0.7-alpha1

2 years ago

2.0.8-alpha1

2 years ago

2.0.6-alpha1

2 years ago

2.0.4-alpha1

2 years ago

2.0.2-alpha1

2 years ago

2.0.0-alpha1

2 years ago

2.0.19-alpha1

2 years ago

2.0.34-alpha1

1 year ago

2.0.33-alpha1

2 years ago

2.0.32-alpha1

2 years ago

2.0.31-alpha1

2 years ago

2.0.30-alpha1

2 years ago

2.0.35-alpha1

1 year ago

2.0.17-alpha1

2 years ago

2.0.16-alpha1

2 years ago

2.0.13-alpha1

2 years ago

2.0.15-alpha1

2 years ago

2.0.14-alpha1

2 years ago

2.0.12-alpha1

2 years ago

2.0.10-alpha1

2 years ago

2.0.11-alpha1

2 years ago

1.1.0

2 years ago

1.0.10-beta.0

2 years ago

1.0.10-beta.2

2 years ago

1.0.10-beta.1

2 years ago

1.0.10-beta.3

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.9-beta.0

3 years ago

1.0.2

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago