Advertisement
Guest User

Jørgen

a guest
May 15th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /******** Sagas: Authentication.tsx **********/
  2.  
  3. import { takeLatest, call, put } from "redux-saga/effects";
  4. import { authenticationActions } from "../actions";
  5. import axios from "axios";
  6. import Cookies from "js-cookie";
  7.  
  8. export function* authenticationSaga() {
  9.     yield takeLatest(authenticationActions.API_AUTH_REQUEST, workerSaga);
  10. }
  11.  
  12. function authenticate(username: string, password: string) {
  13.     return axios({
  14.         method: "post",
  15.         url: "/api/ApplicationUser/Login",
  16.         data: {
  17.             UserName: username,
  18.             Password: password
  19.         }
  20.     });
  21. }
  22.  
  23. function setCookie(name: string, value: string, days: number) {
  24.     var expires = "";
  25.     if (days) {
  26.         var date = new Date();
  27.         date.setTime(date.getTime() + (days*24*60*60*1000));
  28.         expires = "; expires=" + date.toUTCString();
  29.     }
  30.     document.cookie = name + "=" + (value || "")  + expires + "; path=/";
  31. }
  32.  
  33. function getCookie(name: string) {
  34.     var nameEQ = name + "=";
  35.     var ca = document.cookie.split(';');
  36.     for(var i=0;i < ca.length;i++) {
  37.         var c = ca[i];
  38.         while (c.charAt(0)==' ') c = c.substring(1,c.length);
  39.         if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  40.     }
  41.     return null;
  42. }
  43. function eraseCookie(name: string) {  
  44.     document.cookie = name+'=; Max-Age=-99999999;';  
  45. }
  46.  
  47. function* workerSaga(action: any) {
  48.     try {
  49.         const response = yield call(authenticate, action.payload.username, action.payload.password);
  50.  
  51.         // TODO: If we want to set the cookie to be HTTP only to prevent XSS we can't do it from Javascript
  52.         //Cookies.set("authentication_token", "Bearer " + response.data.token, { secure: true, expires: 7, });
  53.        
  54.         setCookie("authentication_token", "Bearer " + response.data, 7);
  55.  
  56.         console.log("Fetching cookie: " + getCookie("authentication_token"));
  57.  
  58.         yield put({ type: authenticationActions.API_AUTH_SUCCESS, response });
  59.     } catch (error) {
  60.         yield put({ type: authenticationActions.API_AUTH_FAILURE, error });
  61.     }
  62. }
  63.  
  64. /******** PROFILEVIEW.TSX ********/
  65.  
  66. import React from 'react';
  67. import {createStyles, makeStyles, Theme, Typography} from "@material-ui/core";
  68. import axios from "axios";
  69.  
  70. const useStyles = makeStyles((theme: Theme) =>
  71.     createStyles({
  72.         root: {
  73.  
  74.         },
  75.     }),
  76. );
  77.  
  78. function getCookie(name: string) {
  79.     var nameEQ = name + "=";
  80.     var ca = document.cookie.split(';');
  81.     for(var i=0;i < ca.length;i++) {
  82.         var c = ca[i];
  83.         while (c.charAt(0)==' ') c = c.substring(1,c.length);
  84.         if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  85.     }
  86.     return null;    
  87. }
  88.  
  89. function getCurrentUser() {
  90.     let config = {
  91.         headers: {
  92.             Authorization: getCookie("authentication_token")
  93.         }
  94.     }
  95.  
  96.     console.log(config);
  97.  
  98.     return axios.get("api/ApplicationUser/GetCurrentUser", config).then((response) => {
  99.         return response;
  100.     });
  101. }
  102.  
  103. function ProfileView() {
  104.     const classes = useStyles();
  105.  
  106.     // Dette fungerer ikke, søk opp hvordan man bruker promises riktig:
  107.     const response = getCurrentUser();
  108.  
  109.     return (
  110.         <div>
  111.             <Typography variant="h4">
  112.             </Typography>
  113.         </div>
  114.     );
  115. }
  116.  
  117. export default ProfileView;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement