# @unologin/web-sdk

> unolog·in web client SDK

Latest version **1.1.0** (published 2023-04-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install @unologin/web-sdk
pnpm add @unologin/web-sdk
yarn add @unologin/web-sdk
bun add @unologin/web-sdk
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2023-04-16 |
| First published | 2023-03-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 75.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Adrian Kriegel |
| Maintainers | adrian-kriegel |

## Links

- npm: https://www.npmjs.com/package/@unologin/web-sdk
- npm.io page: https://npm.io/package/@unologin/web-sdk

## Recent versions

- 1.1.0 (latest) — 2023-04-16
- 1.0.9 — 2023-04-04
- 1.0.8 — 2023-03-30
- 1.0.7 — 2023-03-28
- 1.0.5 — 2023-03-08
- 1.0.4 — 2023-03-06
- 1.0.3 — 2023-03-06
- 1.0.2 — 2023-03-04
- 1.0.1 — 2023-03-04
- 1.0.0 — 2023-03-03

## README

Frontend SDK for interfacing with [unolog·in](https://unolog.in).

This README is a short overview. The full documentation can be found [here](https://unologin.github.io/web-sdk/).

Documentation for other [unolog·in](https://unolog.in) packages can be found [here](https://dashboard.unolog.in/docs).

## Installation

```
npm install @unologin/web-sdk
```

or

```
yarn add @unologin/web-sdk
```

## Typescript

This package includes built-in type declarations. There is no need to install any additional packages.

The below examples will use plain javascript for generality.

## Setup

Before using the library, make sure to connect the SDK with your app via your apps ```appId```.

```javascript
import unologin from '@unologin/web-sdk';

unologin.setup(
  { appId: process.env.UNOLOGIN_APPID }
);
```
## Initiating the login flow

Use the ```startLogin(...)``` function in order to initiate the login flow. 

```javascript
import unologin from '@unologin/web-sdk';

/**
 * Typically, you would call startLogin in
 * the onClick handler of a button.
 * 
 * startLogin returns a promise that 
 * resolves as soon as the popup closes.
 * 
 * startLogin can therefore be awaited.
 */
unologin.startLogin(
  {
    /**
     * user class this 
     * login/registration belongs to 
     */
    userClass: 'users_default',
    /**
     * mode can be 'login' or 'register'
     * or omitted for auto-detect 
     */
    mode: 'register'
  }
)

```

## Handling the login event

```unologin.onLogin``` returns a ```Promise``` which can either be ```await```ed or used with ```.then```/```.catch```.  

```javascript
unologin.startLogin(...).then(() => 
{
  /** 
   * you can now make 
   * authenticated requests! 
   */
  alert('You are now logged in!');
}).catch((e) => 
{
  /* Check the error type. */
  if (e.type === 'ClosedByUser')
  {
    /**
     * Decide what happens if the user
     * quits the login flow.
     */
  }
  else 
  {
    alert('Something went wrong!');
  }
})
```

## Detecting a login cookie

You can use ```unologin.isLoggedIn()``` to check if a user is logged in at any time. This will check for the presence of a javascript-readable state cookie that is set along side the actual login cookie. 

**IMPORTANT:** Do not use ```isLoggedIn()``` or *any* other client-side logic as a security measure. 

*Authentication should always be performed from your backend*.

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