master
Tudor Stanciu 2020-12-23 03:37:26 +02:00
parent 68467cdf4c
commit 9fa3091ab1
5 changed files with 66 additions and 27 deletions

1
.env Normal file
View File

@ -0,0 +1 @@
REACT_APP_IDENTITY_AUTHENTICATION_URL=http://localhost:5063/identity/authenticate?UserName={username}&Password={password}

View File

@ -1,20 +1,27 @@
import React from "react"; import React, { useContext } from "react";
import LoginComponent from "./LoginComponent"; import LoginComponent from "./LoginComponent";
import Button from "@material-ui/core/Button"; import Button from "@material-ui/core/Button";
import { useHistory } from "react-router-dom"; import { authenticate } from "../../../utils/identity";
import {
ApplicationStateContext,
ApplicationDispatchContext
} from "../../../state/ApplicationContexts";
const LoginContainer = () => { const LoginContainer = () => {
const history = useHistory(); const state = useContext(ApplicationStateContext);
const dispatchActions = useContext(ApplicationDispatchContext);
const handleMove = () => { const handleLogin = async () => {
history.push("about"); const { userName, password } = state.credentials;
const token = await authenticate(userName, password);
dispatchActions.onAuthorizationTokenChange(token);
}; };
return ( return (
<> <>
<LoginComponent /> <LoginComponent />
<Button variant="contained" color="primary" onClick={handleMove}> <Button variant="contained" color="primary" onClick={handleLogin}>
Move Login
</Button> </Button>
</> </>
); );

View File

@ -10,6 +10,20 @@ export function reducer(state, action) {
} }
}; };
} }
case "onAuthorizationTokenChange": {
const { token } = action.payload;
return {
...state,
security: {
...state.security,
authorization: {
...state.security.authorization,
token
}
}
};
}
case "onNetworkChange": { case "onNetworkChange": {
const { prop, value } = action.payload; const { prop, value } = action.payload;
return { return {
@ -30,5 +44,7 @@ export const dispatchActions = dispatch => ({
onCredentialsChange: (prop, value) => onCredentialsChange: (prop, value) =>
dispatch({ type: "onCredentialsChange", payload: { prop, value } }), dispatch({ type: "onCredentialsChange", payload: { prop, value } }),
onNetworkChange: (prop, value) => onNetworkChange: (prop, value) =>
dispatch({ type: "onNetworkChange", payload: { prop, value } }) dispatch({ type: "onNetworkChange", payload: { prop, value } }),
onAuthorizationTokenChange: token =>
dispatch({ type: "onAuthorizationTokenChange", payload: { token } })
}); });

View File

@ -9,16 +9,16 @@ function getHeaders() {
return { return {
"Content-Type": "application/json", "Content-Type": "application/json",
Authorization: `Basic ${token}`, Authorization: `Basic ${token.raw}`,
"Accept-Language": `${language}` "Accept-Language": `${language}`
}; };
} }
function internalRequest(url, options) { async function internalRequest(url, options) {
return axios try {
.request(url, options) const res = await axios.request(url, options);
.then(res => res.data) return res.data;
.catch(function (error) { } catch (error) {
if (error.response && error.response.data) { if (error.response && error.response.data) {
throw ( throw (
{ {
@ -31,9 +31,11 @@ function internalRequest(url, options) {
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js // http.ClientRequest in node.js
throw error; throw error;
}); }
} }
export const request = (url, options) => internalRequest(url, options);
export function post(url, data) { export function post(url, data) {
const options = { const options = {
method: "post", method: "post",

13
src/utils/identity.js Normal file
View File

@ -0,0 +1,13 @@
import { request } from "./axios";
export const authenticate = (username, password) => {
const urlTemplate = process.env.REACT_APP_IDENTITY_AUTHENTICATION_URL;
const url = urlTemplate
.replace("{username}", username)
.replace("{password}", password);
const options = {
method: "post"
};
return request(url, options);
};