validate okta token ract

Solutions on MaxInterview for validate okta token ract by the best coders in the world

showing results for - "validate okta token ract"
María Paula
28 May 2018
1import * as React from 'react';
2import { withRouter } from 'react-router-dom';
3import { withAuth } from '@okta/okta-react';
4
5export const AuthContext = React.createContext();
6
7const getUserFromToken = token => {
8  if (token) {
9    try {
10      return JSON.parse(atob(token.split('.')[1]));
11    } catch (error) {
12      // ignore
13    }
14  }
15
16  return null;
17};
18
19export const AuthProvider = ({ children }) => {
20  const [state, setState] = React.useReducer((oldState, newState) => newState, {
21    loading: true,
22    token: undefined,
23    user: null,
24  });
25
26  const updateAuth = async auth => {
27    const token = (await auth.getIdToken()) || null;
28    if (token !== state.token) {
29      setState({
30        token,
31        loading: false,
32        user: getUserFromToken(token),
33      });
34    }
35  };
36
37  return (
38    <AuthContext.Provider value={{ ...state, updateAuth }}>
39      {children}
40    </AuthContext.Provider>
41  );
42};
43
44export const AuthHandler = withAuth(
45  withRouter(({ auth, location }) => {
46    const { updateAuth } = React.useContext(AuthContext);
47
48    React.useEffect(() => {
49      updateAuth(auth);
50    });
51
52    React.useEffect(() => {
53      if (location.pathname === '/login') auth.login('/');
54      if (location.pathname === '/logout') auth.logout('/');
55    }, [auth, location.pathname]);
56
57    return null;
58  }),
59);
60