Sign inSign up

juffalow/user-service

By juffalow

•Updated almost 2 years ago

User management for your web or mobile application.

Image
API management
Web servers
0

615

juffalow/user-service repository overview

⁠User Service

User management for your web or mobile application. Supports both int and uuid primary keys. Allows Local environment where everything is handled by this service (registration, login, access token management, ...) or AWS Cognito where everything is handled by AWS.

This project is Open Source and source code is available on GitHub⁠.

⁠Routes

List of routes with basic info. For testing purpose check Postman collection⁠.

RequestPathDescriptionDataResponse
POST/user/registerRegister new user
{
  "username": string,
  "password": string,
  "meta": {
    "name": string,
    "<key>": <value>
  }
}
GET/auth/user/:idUser detail
{
  "error": null,
  "data": {
    "user": {
      "id": number,
      "meta": {,

      },
      "createdAt": "yyyy-MM-dd mm:HH:ss",
      "updatedAt": "yyyy-MM-dd mm:HH:ss"
    }
  }
}
DELETE/auth/user/:idDelete user
PUT/auth/user/:idUpdate user
POST/user/loginLog in user
{
 "username": string
 "password": string
}
{
  "error": null,
  "data": {
    "user": {
      "id": number,
      "token": string
    }
  }
}
GET/user/logoutLog out user
{
  "error": null,
  "data": null
}
GET/user/refresh-tokenRenew refresh token
{
  "error": null,
  "data": {
    "user": {
      "id": number,
      "token": string
    }
  }
}
DELETE/user/refresh-tokenLog out user
{
  "error": null,
  "data": null
}

⁠Services

⁠Geolocation

Geolocation is used when user logs in to see loacation from where the request came. This can be then used to inform user about new login event with data about the device.

UNKNOWN always return "unknown" for both city and country and can be used for local development or to disable geolocation:

SERVICES_GEOLOCATION_TYPE=UNKNOWN

IPLOCATION uses IP location⁠ service to see geographical location of IP address:

SERVICES_GEOLOCATION_TYPE=IPLOCATION

IPSTACK uses ipstack⁠ and requires accessKey:

SERVICES_GEOLOCATION_TYPE=IPSTACK
SERVICES_GEOLOCATION_IPSTACK_ACCESS_KEY
⁠One Time Password

One Time Passwords are used for two factor authentication.

OTPAUTH uses OTPAuth⁠ package to generate one time passwords:

SERVICES_ONE_TIME_PASSWORD_TYPE=OTPAUTH

SPEAKEASY uses Speakeasy⁠ package to generate one time passwords:

SERVICES_ONE_TIME_PASSWORD_TYPE=SPEAKEASY
⁠Token

Token service is used to create and verify access tokens.

JWT uses JWT⁠:

SERVICES_TOKEN_TYPE=JWT
SERVICES_TOKEN_SECRET=secret
SERVICES_TOKEN_TTL=900

⁠Callbacks

⁠onRegister

When new user successfully register (no duplicate email or other error) the service will notify all subscribed services.

Set in the config:

{
  callbacks: {
    onRegister: [
      type: 'HTTP',
      url: 'https://',
    ],
  },
}

Data:

{
  name: 'user_register',
  user: {
    id: user.id,
    email: user.email,
    meta: user.meta,
  },
  parameters: {
    token: token,
  },
}
⁠onLogin

When user successfully logs in the service will notify all subscribed services.

Set in the config:

{
  callbacks: {
    onLogin: [
      type: 'HTTP',
      url: 'https://',
    ],
  },
}

Data:

{
  name: 'user_login',
  user: {
    id: user.id,
    email: user.email,
    meta: user.meta,
  },
  parameters: {
    os: useragent.os,
    browser: useragent.browser,
    version: useragent.version,
    city: geolocation.city,
    country: geolocation.country,
    ip: ip,
  },
}
⁠onLoginFail
⁠onDelete
⁠onPasswordResetRequest

Data:

{
  name: 'user_password_reset_request',
  user: {
    id: user.id,
    email: user.email,
  },
  parameters: {
    token: verificationToken.token,
  },
}
⁠onPasswordReset

Data:

{
  name: 'user_password_reset',
  user: {
    id: user.id,
    email: user.email,
  },
}

⁠Usage

⁠Docker Compose

Locally in your docker-compose.yml:

services:
    mysql_user_service:
        container_name: db_user_service
        image: mysql
        environment:
            - MYSQL_DATABASE=user_service
            - MYSQL_USER=user_service
            - MYSQL_PASSWORD=password
            - MYSQL_ROOT_PASSWORD=password
            - MYSQL_PORT=3306
        ports:
            - 3306:3306
        volumes:
            - 'mysql_user_service_data:/data/db'
        
    user_service:
        image: juffalow/user-service:latest
        depends_on:
            - mysql_user_service
        restart: unless-stopped
        ports:
            - 3010:3010
        environment:
            - PORT=3010
            - DOMAIN=localhost
            - ROUTE_PREFIX=/auth
            - CORS_ORIGINS=localhost,example.com
            - LOGGER_LEVEL=info
            - DATABASE_HOST=mysql_user_service
            - DATABASE_NAME=user_service
            - DATABASE_USER=user_service
            - DATABASE_PASSWORD=password
            - DATABASE_PORT=3306
            - SERVICES_TOKEN_TYPE=JWT
            - SERVICES_TOKEN_SECRET=secret
            - SERVICES_TOKEN_TTL=900
            - SERVICES_GEOLOCATION_TYPE=UNKNOWN
        links:
            - mysql_user_service
      
volumes:
    mysql_user_service_data:
⁠NGINX
server {
  listen 80;
  listen [::]:80;

  server_name _;

  location / {
    proxy_set_header  X-Forwarded-For $remote_addr;

    proxy_pass  http://localhost:3010;
  }
}

Tag summary

Content type

Image

Digest

sha256:b871d5991…

Size

86.8 MB

Last updated

almost 2 years ago

docker pull juffalow/user-service