---
# System prepended metadata

title: Marvin auth kit

---

# Marvin auth kit

How to use marvin-auth-kit

## Initialise

```typescript=
// auth.ts
import AuthKit from 'marvin-auth-kit';

const authKit = new AuthKit('http://zandbakvlooys-swm01.marvin.vito.local:8080/auth')

export authKit;

```

of 

```typescript=
// index.ts

// auth.service.ts
import AuthKit from 'marvin-auth-kit';
AuthKit.config('http://zandbakvlooys-swm01.marvin.vito.local:8080/auth');

export default AuthKit;


// login.container.tsx

import AuthKit from 'auth.service';
...
function handleLogin(username, password) {
    AuthKit.login(username, password);
}
```

## Authentication related functions

```typescript=
// app.ts
import { authKit } from './auth';

// login with username & password, to retreive token
// this token is also persisted in the auth instance and used for future requests
authKit.login(username, password)
.then(() => {
    // success, continue with 
})
.catch(() => {
    // catch login error
});

// remove token and thereby active session
authKit.logout();
```

Een token is maar geldig voor een bepaald timeframe. Wanneer een token verlopen is, worden nieuwe tokens opgevraagd dmv een refresh request. Op basis van het `refreshToken` wordt een nieuw `accessToken` en `refreshToken` opgevraagd.

Dit refresh mechanisme is ingebakken in de auth library en doormiddel van interceptors toegepast op HTTP requests.


## Register

```typescript=
// app.ts
import { authKit } from './auth';

// register a new user
authKit.register.request(email, password);

// confirm a new users registration
authKit.register.confirm(confirmToken);
```


## Email

```typescript=
// app.ts
import { authKit } from './auth';

// request a users email change url via email
authKit.email.request(email);

// confirm a new users email
authKit.email.confirm(confirmToken);
```



## Password

```typescript=
// app.ts
import { authKit } from './auth';

// request a users password change url via email
authKit.password.request(email);

// confirm a new users password
authKit.password.confirm(confirmToken, password);
```


## Roles

```typescript=
// app.ts
import { authKit } from './auth';

// CRUD a role
authKit.roles.create(role);
authKit.roles.update(role);
authKit.roles.destroy(role);
authKit.roles.query({ offset: 5, limit: 10});
```


## Users

```typescript=
// app.ts
import { authKit } from './auth';

// CRUD a role
authKit.users.create(user);
authKit.users.read(email);
authKit.users.update(user);
authKit.users.destroy(user);
authKit.users.query({ offset: 5, limit: 10, role: 'admin'});
```