项目作者: timoye

项目描述 :
Simple Laravel API Authentication based on Laravel Sanctum
高级语言: PHP
项目地址: git://github.com/timoye/patritia.git
创建时间: 2020-09-24T03:44:10Z
项目社区:https://github.com/timoye/patritia

开源协议:

下载


Introduction

This is a simple Auth Service based on Laravel.

Token is generated in a json response from the Register, Login or Renew Token endpoint.

After token is generated, it is used in the Header for protected requests. See below

Headers for endpoints that requires token

  1. Authorization: Bearer API Token Generated
  2. Accepts: application/json

Setup

  • Pull this repo
  • Run composer install
  • Copy .env.example to .env
  • Setup database by creating sqlite.database file in database directory (Or setup MySQL if you wish)
  • Run php artisan migrate
  • Run php artisan serve
  • Register a user using endpoint or use Postman Collection in Endpoints

Endpoints

There are 4 API Endpoints

Route Name Endpoint Type Details
Register /api/register POST Unprotected
Login /api/login POST Unprotected
Renew Token /api/renew-token GET Requires Token
User Data /api/user-data GET Requires Token

Download this Postman Collection file of all requests

Register

Register endpoint accepts 3 parameters

  1. name | required
  2. email | unique to a user and required
  3. password | required

Successful Register response

  1. Status code 200
  2. {
  3. "status": "success",
  4. "message": "Successfully Registered",
  5. "token": "1|yba3MVcRCFmQ2CaEnikKkuXoiXaBMuzNv1UaZiZe"
  6. }

Unsuccessful Register response

  1. Status code 422
  2. {
  3. "message": "The given data was invalid.",
  4. "errors": {
  5. "name": [
  6. "The name field is required."
  7. ],
  8. "email": [
  9. "The email has already been taken."
  10. ]
  11. }
  12. }
  13. OR
  14. Status code 200
  15. {
  16. "status": "fail",
  17. "message": "Something went wrong"
  18. }

Login

Login endpoint accepts 2 parameters

  1. email | required
  2. password | required

Successful Login response

  1. Status code 200
  2. {
  3. "status": "success",
  4. "message": "Successfully Authenticated",
  5. "token": "4|Fz4qLAbXpAnlSy6wd7YwWCDvypCUftVc629fqYP8"
  6. }

Unsuccessful Login response

  1. Status code 403
  2. {
  3. "status": "fail",
  4. "message": "unauthenticated"
  5. }

Renew Token

Renew Token endpoint only requires the Header Authorization parameters

  1. Authorization: Bearer API Token Generated

Successful Renew Token response

  1. Status code 200
  2. {
  3. "status": "success",
  4. "message": "Successfully Renewed Token",
  5. "token": "6|xqY7kJVnRUhRm9b4P9rKmTEnXvC8U98QTzLJcWCK"
  6. }

Unsuccessful Renew Token response

  1. Status code 200
  2. {
  3. "status": "fail",
  4. "message": "Something went wrong"
  5. }

User Data

User Data endpoint only requires the Header Authorization parameters

  1. Authorization: Bearer API Token Generated

Successful User Data response

  1. Status code 200
  2. {
  3. "id": 1,
  4. "name": "Tim",
  5. "email": "tim@gmail.com",
  6. "email_verified_at": null,
  7. "created_at": "2020-09-25T02:26:55.000000Z",
  8. "updated_at": "2020-09-25T02:26:55.000000Z"
  9. }

Unsuccessful User Data response

  1. Status code 200
  2. {
  3. "status": "fail",
  4. "message": "Something went wrong"
  5. }

Tests

To run tests, run

  1. php artisan test