1.1.0 • Published 6 months ago

otentikasi v1.1.0

Weekly downloads
-
License
ISC
Repository
-
Last release
6 months ago

Otentikasi

Install

npm install otentikasi

Usage

Set Auth Config

import { AuthConfig } from 'otentikasi';

AuthConfig.setSecret('something-secret');
AuthConfig.setOptions({
  expiresIn: '15m',
});

Create user interface

interface User {
  id: number;
  email: string;
  password: string;
}

Create auth class

import { Auth, RegisterCredential } from 'otentikasi';

class MyAuth extends Auth<User> {
  protected async createUser(
    credential: RegisterCredential<User>,
  ): Promise<User> {
    return await this.userRepository.create(credential);
  }
  protected async findUserByEmail(email: string): Promise<User> {
    return await this.userRepository.find({ email });
  }
  protected async findUserById(id: number): Promise<User | null> {
    return await this.userRepository.findById(id);
  }
}

Create auth object

const myAuth = new MyAuth();

Register, return AuthResult

// return `AuthResut`
const user = await myAuth.register({
  email: 'user@email.com',
  password: 'password',
});

Login, return AuthResult

// return `AuthResult`
const user = await myAuth.login({
  email: 'user@gmail.com',
  password: 'password',
});

Authenticate, accept jwt string, return user object

// return `User`
const user = await myAuth.authenticate('ed****');

AuthResult.getToken() return jwt token.

// return jwt
await newUser.getToken();

Errors

Throws AuthError object.

import { AuthError } from 'otentikasi';

try {
  // login, register, authenticate
} catch (err) {
  if (err instanceof AuthError) {
    err.name; // REGISTER_ERROR' | 'LOGIN_ERROR' | 'AUTHENTICATE_ERROR
    err.message; // string
    err.cause; // any
  }
}
1.0.2

8 months ago

1.1.0

6 months ago

1.0.4

8 months ago

1.0.3

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago