@rockr/sso v1.1.0
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 tokenloginfailure
– Triggered when an error occurs while logging in. Returns the given error objectlogout
– 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 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago