I'm working on a simple frontend in react js and a backend in rails. The frontend is made up of 2 parts :
- Some pages (/home, /about, ...) which do not need a login but it needs to get data from the rails-api. To make things secure I added an authentication in the frontend code below : (i don't know whether that fits best practices).
...
const url = endpoint+"tokens"
const credentials = {user: {email: "frontend@email.com", password: "mypassword" }}
const result = await axios.post(url, JSON.stringify(credentials), {
headers: {
'Content-Type': 'application/json'
}
});
authorization = { key : result.data.token , expiredTime : result.data.expirationTime}
...
- Dashboard (/login) with login page to manage the database content
The rails backend Controllers check every request with 'before_action :check_login' to make sur that user is logged in before execute any action on the database.