- Instalation
- API References
- Error Handling - TODO
- Types - TODO
Install with npm
git clone https://github.com/pawix135/express-jwt-auth.git
cd express-jwt-auth
npm install
Change .env.example
in the root of the directory to .env
and replace variables with corresponding values.
NODE_ENV=<ENVIRONMENT_TYPE> # production | development
PORT=<SERVER_PORT> # 8080
JWT_ACCESS_SECRET=<ACCESS_TOKEN_SECRET> # openssl rand -base64 32
JWT_REFRESH_SECRET=<REFRESH_TOKEN_SECRET> # openssl rand -base64 32
DATABASE_URL=<DATABASE_URL> # Your Postgres database provider url
Geneare Prisma types and create migration.
npx prisma generate
npx prisma migrate dev --name init
Run development server
npm run dev
Build and run - TODO
npm run build
node ./dist/server.js
The Prisma ORM is built on top of Postgres database. Right now there's only one model.
model User {
id Int @id @default(autoincrement())
username String @unique
hash String
email String? @unique
}
Create new user account
POST /api/auth/signup HTTP/1.1
Content-Type: application/json
interface AuthSignUpBody {
username: string;
password: string;
}
interface AuthSignUpResponse {
auth: boolean,
error?: APIError;
}
Signs in user and sets authorization header for access token(30min) and cookie for refresh token(30 days).
POST /api/auth/signin HTTP/1.1
Content-Type: application/json
interface AuthSignInBody {
username: string;
password: string;
}
interface AuthSignUpResponse {
access_token: string;
auth: boolean;
error?: APIError;
}
Revoke access token
POST /api/auth/revoke HTTP/1.1
Content-Type: application/json
Cookie: <refresh_token>
interface AuthRevokeResponse {
access_token: string;
auth: boolean;
error?: APIError;
}
Return user
GET /api/user/me HTTP/1.1
Authorization: Bearer <access_token>
interface UserMeResponse {
ok: boolean;
me: User;
error?: APIError;
}
Update selected user settings
POST /api/user/settings HTTP/1.1
Authorization: Bearer <access_token>
interface UserChangeSettingsBody{
username?: string;
email?: string;
password?: string;
}
interface UserChangeSettingsResponse {
ok: boolean;
success: boolean;
error?: APIError;
}
Update user username
POST /api/user/settings/username HTTP/1.1
Authorization: Bearer <access_token>
interface UserChangeUsernameBody{
username: string;
}
interface UserChangeUsernameResponse {
ok: boolean;
success: boolean;
error?: APIError;
}
Update user email
POST /api/user/settings/email HTTP/1.1
Authorization: Bearer <access_token>
interface UserChangeEmailBody{
email: string;
}
interface UserChangeEmailResponse {
ok: boolean;
success: boolean;
error?: APIError;
}
Update user password
POST /api/user/settings/password HTTP/1.1
Authorization: Bearer <access_token>
interface UserChangePasswordBody{
password: string;
}
interface UserChangePasswordResponse {
ok: boolean;
success: boolean;
error?: APIError;
}