Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******** Sagas: Authentication.tsx **********/
- import { takeLatest, call, put } from "redux-saga/effects";
- import { authenticationActions } from "../actions";
- import axios from "axios";
- import Cookies from "js-cookie";
- export function* authenticationSaga() {
- yield takeLatest(authenticationActions.API_AUTH_REQUEST, workerSaga);
- }
- function authenticate(username: string, password: string) {
- return axios({
- method: "post",
- url: "/api/ApplicationUser/Login",
- data: {
- UserName: username,
- Password: password
- }
- });
- }
- function setCookie(name: string, value: string, days: number) {
- var expires = "";
- if (days) {
- var date = new Date();
- date.setTime(date.getTime() + (days*24*60*60*1000));
- expires = "; expires=" + date.toUTCString();
- }
- document.cookie = name + "=" + (value || "") + expires + "; path=/";
- }
- function getCookie(name: string) {
- var nameEQ = name + "=";
- var ca = document.cookie.split(';');
- for(var i=0;i < ca.length;i++) {
- var c = ca[i];
- while (c.charAt(0)==' ') c = c.substring(1,c.length);
- if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
- }
- return null;
- }
- function eraseCookie(name: string) {
- document.cookie = name+'=; Max-Age=-99999999;';
- }
- function* workerSaga(action: any) {
- try {
- const response = yield call(authenticate, action.payload.username, action.payload.password);
- // TODO: If we want to set the cookie to be HTTP only to prevent XSS we can't do it from Javascript
- //Cookies.set("authentication_token", "Bearer " + response.data.token, { secure: true, expires: 7, });
- setCookie("authentication_token", "Bearer " + response.data, 7);
- console.log("Fetching cookie: " + getCookie("authentication_token"));
- yield put({ type: authenticationActions.API_AUTH_SUCCESS, response });
- } catch (error) {
- yield put({ type: authenticationActions.API_AUTH_FAILURE, error });
- }
- }
- /******** PROFILEVIEW.TSX ********/
- import React from 'react';
- import {createStyles, makeStyles, Theme, Typography} from "@material-ui/core";
- import axios from "axios";
- const useStyles = makeStyles((theme: Theme) =>
- createStyles({
- root: {
- },
- }),
- );
- function getCookie(name: string) {
- var nameEQ = name + "=";
- var ca = document.cookie.split(';');
- for(var i=0;i < ca.length;i++) {
- var c = ca[i];
- while (c.charAt(0)==' ') c = c.substring(1,c.length);
- if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
- }
- return null;
- }
- function getCurrentUser() {
- let config = {
- headers: {
- Authorization: getCookie("authentication_token")
- }
- }
- console.log(config);
- return axios.get("api/ApplicationUser/GetCurrentUser", config).then((response) => {
- return response;
- });
- }
- function ProfileView() {
- const classes = useStyles();
- // Dette fungerer ikke, søk opp hvordan man bruker promises riktig:
- const response = getCurrentUser();
- return (
- <div>
- <Typography variant="h4">
- </Typography>
- </div>
- );
- }
- export default ProfileView;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement