laravel-react-booklyb-api is a REST API written with Laravel framework to serve as backend for react-booklyb
The project demonstrates the use of REST API in laravel that can be used across any frontend platform. Laravel sanctum is used for authentication which allows each user of the application to generate multiple API tokens for their account. These tokens are used to grant users access to features to the APIs.
The base URL to consume the API is https://peaceful-cove-38084.herokuapp.com/api/
which can be used with axios
.
In addition, you should enable the withCredentials
option on your global axios
instance. like below:
Axios.defaults.baseURL = "https://peaceful-cove-38084.herokuapp.com/api/";
Axios.defaults.withCredentials = true;
The user's authorization token is used in the configuration below with the fetch API like Axios
to access features.
const config = {
headers: {
Authorization: `Bearer ${$userToken}`
}
};
For Authenticating a user, the user table consists of three required colums which are name
, email
, and password
.
- Signing up a user requires using the URL
https://peaceful-cove-38084.herokuapp.com/api/sign-up
with apost
request.
Axios.post(`sign-up`, values);
the values
option is the values of the field the user submits i.e name
, email
, password
.
This returns a json object with the following values.
"status": 200,
"message": "Sign up Successful",
"token": $userToken, // token generated after signing up a user or logging in
"username": $username // user's name
- Logging in a user uses the
https://peaceful-cove-38084.herokuapp.com/api/login
URL with apost
request.
Axios.post(`login`, values);
This returns a json object with the following values similar to the sign-up values.
"status": 200,
"message": "Sign up Successful",
"token": $userToken, // token generated after signing up a user or logging in
"username": $username // user's name
- To get a user's name we utilize the
https://peaceful-cove-38084.herokuapp.com/api/get-user
with aget
request which returns a user'sname
.
Axios.get(`get-user/`, config);
- Logging out a user uses the
https://peaceful-cove-38084.herokuapp.com/api/logout
URL with aget
request andconfig
.
Axios.get(`logout/`, config);
The following columns in the book table bookTitle
, bookAuthor
, bookCategory
, comment
, numberOfPages
, currentPageRead
, currentChapterTitle
, and currentChapterRead
can be populated with the user's inputs.
The bookTitle
and bookCategory
field are required while the rest are nullable.
- Adding a book requires using the URL
https://peaceful-cove-38084.herokuapp.com/api/add-book
with apost
request.
Axios.post(`add-book`, values, config);
This returns a json object with the following values.
"status": 200,
"message": "Book Added Successfully",
"book": $book
The $book
variable from above contains the following values: bookId
, bookTitle
, bookAuthor
, bookCategory
, comment
, numberOfPages
, currentPageRead
, currentChapterTitle
, and currentChapterRead
where bookId
is a unique string generated for each books.
- To Update a book we require using the URL
https://peaceful-cove-38084.herokuapp.com/api/update-book
with aput
request.
Axios.post(`update-book`, values, config);
This returns a json object with the following values.
"status": 200,
"message": "Book Updated Successfully",
- To get all the books by a single user we use the
https://peaceful-cove-38084.herokuapp.com/api/books
with aget
request.
Axios.get(`books`, config);
This returns the variables bookId
, bookTitle
, bookAuthor
, bookCategory
, comment
, numberOfPages
, currentPageRead
, currentChapterTitle
, and currentChapterRead
of each books by a single user.
- To get a single book by a single user we use the
https://peaceful-cove-38084.herokuapp.com/api/books/${bookId}
with aget
request.
Axios.get(`books/${bookId}`, config);
This returns the variables bookId
, bookTitle
, bookAuthor
, bookCategory
, comment
, numberOfPages
, currentPageRead
, currentChapterTitle
, and currentChapterRead
of a book with unique identity of bookId
.
- To delete a single book by a user we use the
https://peaceful-cove-38084.herokuapp.com/api/delete-book/${bookId}
with adelete
request using the unique identitybookId
.
Axios.delete(`delete-book/${bookId}`, config);
- Sign in user
- Authenticate user
- Logging out user
- Add Book feature
- Get Books feature
- Edit Book feature
- Delete Book feature
-
PHP
-
PHP Code_Sniffer
-
PHP Code Beautifier
- Clone the project
git clone https://github.com/adejam/laravel-react-booklyb-api.git
- Install Dependencies
composer require
To check for errors on PHP
composer phpcs
Or to beautify PHP codes
composer phpcs
- Github: @adejam
- Twitter: @adeleye_oj
- LinkedIn: @adeleye-jamiu
Contributions, issues and feature requests are welcome!
Feel free to check the issues page.
Give a ⭐️ if you like this project!