Role authorization for Netlify-auth (#224)

This commit is contained in:
Cássio Souza
2017-01-26 19:23:42 -02:00
committed by GitHub
parent 922ecbdf4d
commit ecbcbf06b5
6 changed files with 279 additions and 244 deletions

View File

@ -32,6 +32,23 @@ export function logout() {
};
}
// Check if user data token is cached and is valid
export function authenticateUser() {
return (dispatch, getState) => {
const state = getState();
const backend = currentBackend(state.config);
dispatch(authenticating());
return backend.currentUser()
.then((user) => {
if (user) dispatch(authenticate(user));
})
.catch((error) => {
dispatch(authError(error));
dispatch(logoutUser());
});
};
}
export function loginUser(credentials) {
return (dispatch, getState) => {
const state = getState();
@ -41,6 +58,9 @@ export function loginUser(credentials) {
return backend.authenticate(credentials)
.then((user) => {
dispatch(authenticate(user));
})
.catch((error) => {
dispatch(authError(error));
});
};
}

View File

@ -1,7 +1,6 @@
import yaml from "js-yaml";
import { set, defaultsDeep } from "lodash";
import { currentBackend } from "../backends/backend";
import { authenticate } from "../actions/auth";
import { authenticateUser } from "../actions/auth";
import * as publishModes from "../constants/publishModes";
export const CONFIG_REQUEST = "CONFIG_REQUEST";
@ -85,11 +84,7 @@ export function loadConfig() {
.then(applyDefaults)
.then((config) => {
dispatch(configDidLoad(config));
const backend = currentBackend(config);
return backend && backend.currentUser();
})
.then((user) => {
if (user) dispatch(authenticate(user));
dispatch(authenticateUser());
});
}).catch((err) => {
dispatch(configFailed(err));