{"id":26365093,"url":"https://github.com/dangkhoa2016/rails-8-api-authentication","last_synced_at":"2025-03-16T19:30:13.817Z","repository":{"id":273163162,"uuid":"918125679","full_name":"dangkhoa2016/Rails-8-API-Authentication","owner":"dangkhoa2016","description":"Simple Rails v8 API server with JWT-based authentication using devise gem","archived":false,"fork":false,"pushed_at":"2025-03-03T06:44:26.000Z","size":79,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-03T07:28:53.800Z","etag":null,"topics":["devise","devise-jwt","jwt-authentication"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dangkhoa2016.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-17T09:52:20.000Z","updated_at":"2025-03-03T06:44:29.000Z","dependencies_parsed_at":null,"dependency_job_id":"ffa0efb9-0bdc-43b3-ad4c-d4cefd821b52","html_url":"https://github.com/dangkhoa2016/Rails-8-API-Authentication","commit_stats":null,"previous_names":["dangkhoa2016/rails-8-api-authentication"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dangkhoa2016%2FRails-8-API-Authentication","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dangkhoa2016%2FRails-8-API-Authentication/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dangkhoa2016%2FRails-8-API-Authentication/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dangkhoa2016%2FRails-8-API-Authentication/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dangkhoa2016","download_url":"https://codeload.github.com/dangkhoa2016/Rails-8-API-Authentication/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243919755,"owners_count":20368943,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["devise","devise-jwt","jwt-authentication"],"created_at":"2025-03-16T19:30:12.845Z","updated_at":"2025-03-16T19:30:13.810Z","avatar_url":"https://github.com/dangkhoa2016.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\n# Rails v8 API Authentication with JWT\n\nThis is a simple Rails v8 API server with JWT-based authentication. The server handles user registration, login, profile updates, and user management with role-based access.\n\n\n## Features\n\n- **User Registration:**\n  - Fields: `email`, `password`, `username`\n  - Validation: `email` and `username` are unique, `password` is required.\n\n- **User Login:**\n  - Fields: `username`, `password`\n  - Returns a JWT token upon successful login.\n\n- **User Logout:**\n  - Invalidates the JWT token on the client side.\n\n- **Get User Info:**\n  - Retrieves information for the logged-in user.\n  - Admins can also view information for other users.\n\n- **Update User Info (Basic):**\n  - Allows a user to update their profile information (e.g., email, username).\n\n- **Update User Role (Admin Only):**\n  - Admins can update the `role` of a user (e.g., admin, regular user).\n\n- **Delete User (Self-Delete):**\n  - A user can delete their own account.\n\n- **Delete User (Admin Only):**\n  - Admins can delete any user.\n\n\n## Technologies Used\n\n- **Ruby on Rails v8**: Web framework.\n- **SQLite/PostgreSQL**: Database (SQLite used in the example).\n- **devise**: Flexible authentication solution for Rails with Warden.\n- **devise JWT**: ForJWT token authentication with devise and rails.\n\n## Setup\n\n### 1. Install Rails\n\nIf you don’t have Rails 8 installed yet, run:\n```bash\ngem install rails -v 8\n```\n\n## Installation\n\n1. Clone the repository:\n    ```bash\n    git clone \u003crepository-url\u003e\n    cd \u003crepository-folder\u003e\n    ```\n\n2. Install dependencies:\n    ```bash\n    bundle install\n    ```\n\n3. Create a `.env` file at the root of your project for environment variables:\n    ```env\n    RAILS_LOG_TO_STDOUT=true\n    RAILS_ENV=development\n    PORT=4000\n    RAILS_MAX_THREADS=1\n    PORT=3000\n    ```\n\n## API Endpoints\n\n### 1. **POST /register**\n- Registers a new user.\n- **Body**:\n    ```json\n    {\n      \"email\": \"user@example.com\",\n      \"password\": \"password123\",\n      \"username\": \"user123\"\n    }\n    ```\n- **Response**:\n    ```json\n    {\n      \"message\": \"User created successfully.\"\n    }\n    ```\n\n### 2. **POST /login**\n- Logs in an existing user and returns a JWT token.\n- **Body**:\n    ```json\n    {\n      \"username\": \"user123\",\n      \"password\": \"password123\"\n    }\n    ```\n- **Response**:\n    ```json\n    {\n      \"token\": \"\u003cjwt_token\u003e\",\n      \"message\": \"Login successful\",\n      \"user\": {\n        \"username\": \"user123\",\n        ...\n      }\n    }\n    ```\n\n### 3. **POST /logout**\n- Logs out the user by invalidating their token.\n- **Response**:\n    ```json\n    {\n      \"message\": \"Logout successful.\"\n    }\n    ```\n\n### 4. **GET /user**\n- Retrieves the logged-in user's information.\n- **Headers**:\n    - `Authorization`: `Bearer \u003cjwt_token\u003e`\n- **Response**:\n    ```json\n    {\n      \"username\": \"user123\",\n      \"email\": \"user@example.com\",\n      \"role\": \"user\"\n    }\n    ```\n\n### 5. **PUT /user**\n- Updates basic information of the logged-in user (email or username).\n- **Headers**:\n    - `Authorization`: `Bearer \u003cjwt_token\u003e`\n- **Body**:\n    ```json\n    {\n      \"email\": \"new_email@example.com\",\n      \"username\": \"new_username\"\n    }\n    ```\n- **Response**:\n    ```json\n    {\n      \"message\": \"User information updated successfully.\"\n    }\n    ```\n\n### 6. **DELETE /user**\n- Deletes the logged-in user account.\n- **Headers**:\n    - `Authorization`: `Bearer \u003cjwt_token\u003e`\n- **Response**:\n    ```json\n    {\n      \"message\": \"Bye! Your account has been successfully cancelled. We hope to see you again soon.\"\n    }\n    ```\n\n### 7. **DELETE /user/2**\n- Deletes a user account (only accessible by admin).\n- **Headers**:\n    - `Authorization`: `Bearer \u003cjwt_token\u003e`\n- **Body**:\n    ```json\n    {\n    }\n    ```\n- **Response**:\n    ```json\n    {\n      \"message\": \"User deleted successfully.\"\n    }\n    ```\n\n## Example Usage\n\n1. Register a user:\n    ```bash\n    curl -X POST http://localhost:4000/users/register -H \"Content-Type: application/json\" -d '{\"email\": \"user@example.com\", \"password\": \"password123\", \"username\": \"user123\"}'\n    ```\n\n2. Log in to get the JWT token:\n    ```bash\n    curl -X POST http://localhost:4000/users/login -H \"Content-Type: application/json\" -d '{\"username\": \"user123\", \"password\": \"password123\"}'\n    ```\n\n3. Get user information:\n    ```bash\n    curl -X GET http://localhost:4000/user/me -H \"Authorization: Bearer \u003cjwt_token\u003e\"\n    ```\n\nfor more information, please check the [registration.sh](./manual/registration.sh), [session.sh](./manual/session.sh) and [user.sh](./manual/user.sh) file.\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdangkhoa2016%2Frails-8-api-authentication","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdangkhoa2016%2Frails-8-api-authentication","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdangkhoa2016%2Frails-8-api-authentication/lists"}