bun installbun run src/app.ts- Create a
.envfile and add the following to it
CORS_ORIGN=<client@example.com>
MONGO_URI=<path/to/mongo_db_database>
JWT_SECRET=<SOME_IMPORTANT_SECRET>- All API paths start at
/api/v1 - Auth routes
/register,/sign-inand/logoutare accessed through the/authroute fragment. - Room routes mounted on
/rooms. - User actions are mounted on
/user
A user is allowed to register, sign-in or logout
- A post request to
/registercontaining ausernameandpasswordfield in the request body. N.B => Cors and credentials must be properly implemented - For
/sign-inthe same params as register above are required in the request body, aAuthorizationcookie is sent back to the client. No headers are set here - A post request to '/logout' unsets the
Authorizationcookie that mught have been previously set by/registetor/sign-inroute thereby invalidating the user's auth. - The Authentication by verifying the token with a middleware on each request to a protected route.
To consume the API you send a request to the server @SERVER_URI/api/v1/<action> where is either a room, user or auth action.
E.g to get all the rooms in which a user is a participant using the fetch API, we do:
const res = await fetch('http://example.com/api/v1/user/rooms', {
method: 'GET',
headers: {'Content-Type': 'application/json'},
mode: 'cors', // important for cors
credentials: 'include' // important for sending stuff like cookies using cors
});
const data = await res.json();
/* Note that you need to check the data for an "errMssg" key as that's where any errors are passed into*/
if(data["errMssg"]) console.error(data["errMssg"]);
else {/* Update state here maybe set a list or rooms or something */}E.g. to access the register route we send a POST request to <domain_name.com>/api/v1/auth/register/
- Validation Errors aren't handled correctly.