nazirov91 / ra-strapi-rest

React Admin data provider for Strapi.js
125 stars 40 forks source link

updated/alt auth example #16

Closed NukaCody closed 4 years ago

NukaCody commented 4 years ago

Thanks a lot for the work you put into this, it's making my life a lot easier for building a small internal app. When it came to following your auth provider, the examples were slightly off for the versions I was running. For example, the user's plugin directory structure has changed ever so slightly

I'm using: Strapi - 3.0.0-beta.19.3 React-Admin - 3.3.0

I was able to get it working with

//  App.js
const httpClient = (url, options = {}) => {
  if (!options.headers) {
    options.headers = new Headers({ Accept: 'application/json' })
  }
  const token = Cookies.getCookie('token') // <-- Change localstorage to the cookie helper
  options.headers.set('Authorization', `Bearer ${token}`)
  return fetchUtils.fetchJson(url, options)
}
// authProvider.js
import Cookies from './helpers/Cookies'

export default {

    login: ({ username, password }) => {
        const identifier = username // <-- Changing username to match Strapi's identifier requirement
        const request = new Request('http://localhost:1337/auth/local', {
            method: 'POST',
            body: JSON.stringify({ identifier, password }),
            headers: new Headers({ 'Content-Type': 'application/json'})
        });
        return fetch(request)
            .then(response => {
                if (response.status < 200 || response.status >= 300) {
                    throw new Error(response.statusText);
                }
                return response.json();
            })
            .then(response => {
                Cookies.setCookie('token', response.jwt, 1);
                Cookies.setCookie('role', response.user.role.name, 1);
            });
    },

    logout: () => {
        Cookies.deleteCookie('token');
        Cookies.deleteCookie('role');
        return Promise.resolve();
    },

    checkAuth: () => {
        return Cookies.getCookie('token') ? Promise.resolve() : Promise.reject();
    },

    checkError: ({ status }) => {
        if (status === 401 || status === 403) {
            Cookies.deleteCookie('token');
            Cookies.deleteCookie('role');
            return Promise.reject();
        }
        return Promise.resolve();
    },

    getPermissions: () => {
        const role = Cookies.getCookie('role');
        return role ? Promise.resolve(role) : Promise.reject();
    },
}

This may just fit my use case, but just wanted to make sure to help others if they come across this issue. Let me know if you would like this issue as a pull request instead.

nazirov91 commented 4 years ago

Hi @codypenta,

I am glad it was useful for your project :) Yes, I have noticed. It has been some time since I updated the Readme file. If it is not too much trouble for you, please go ahead and submit a PR.

Thank you!

NukaCody commented 4 years ago

Thanks! Pull request submitted: #17