# @rockr/sso

> ## Instalation

Latest version **1.1.0** (published 2022-04-07) · ISC license · 0 weekly downloads

## Install

```sh
npm install @rockr/sso
pnpm add @rockr/sso
yarn add @rockr/sso
bun add @rockr/sso
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2022-04-07 |
| First published | 2021-04-06 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 114.1 KB |
| Known vulnerabilities | 0 (+24 in 2 direct dependencies) |
| Install scripts | no |
| Author | RockOS Team |
| Maintainers | tuliocmp, andrey.leontyev, stageteam, ph-gomes, renatojunior-rockcontent, felipeferrarini, benitsrock, gabrielrockr, felipe.oliveira, henriqueweiand, maracaipe, kelvin.chaves, rockcontentfin, lucasinoue |

## Links

- npm: https://www.npmjs.com/package/@rockr/sso
- npm.io page: https://npm.io/package/@rockr/sso

## Dependencies (2)

- [axios](https://npm.io/package/axios.md) ^0.21.0
- [js-cookie](https://npm.io/package/js-cookie.md) ^2.2.1

## Recent versions

- 1.1.0 (latest) — 2022-04-07
- 2.0.35-alpha1 (alpha) — 2022-11-14
- 1.0.10-beta.3 (beta) — 2022-01-18
- 2.0.34-alpha1 — 2022-11-14
- 2.0.33-alpha1 — 2022-10-28
- 2.0.32-alpha1 — 2022-10-28
- 2.0.31-alpha1 — 2022-10-26
- 2.0.30-alpha1 — 2022-10-26
- 2.0.29-alpha1 — 2022-10-26
- 2.0.28-alpha1 — 2022-10-26
- 2.0.27-alpha1 — 2022-10-25
- 2.0.26-alpha1 — 2022-10-25
- 2.0.25-alpha1 — 2022-10-25
- 2.0.24-alpha1 — 2022-10-24
- 2.0.23-alpha1 — 2022-10-24
- … 41 more at https://npm.io/package/@rockr/sso/versions

## README

# RockOS SSO

## Instalation

`npm i @rockr/sso`

## Usage

```ts
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

```ts
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

```ts
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

```ts
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.

```ts
// 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.

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

```ts
sso.loginWithGoogle()
```


### `.loginWithFacebook`

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

```ts
sso.loginWithFacebook()
```


### `.loginWithLinkedin`

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

```ts
sso.loginWithLinkedin()
```

---
_Source: https://npm.io/package/@rockr/sso · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
