MCPcopy Index your code
hub / github.com/Packstack-Tech/packstack / AppProvider

Class AppProvider

frontend/src/AppContext.tsx:46–106  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

44const Consumer = AppContext.Consumer;
45
46export class AppProvider extends React.Component<AppProviderProps, AppProviderState> {
47 token: TokenInterface;
48
49 constructor(props: AppProviderProps) {
50 super(props);
51 this.token = props.token != null ? props.token : new Token();
52 this.state = {
53 api: new Api(new Token()),
54 jwt: {
55 decoded: this.token.getDecoded(),
56 auth_token: this.token.getString()
57 },
58 userInfo: null,
59 categories: [],
60 setAuthToken: this.setAuthToken,
61 fetchUser: this.fetchUser,
62 logout: this.logout
63 };
64 }
65
66 componentDidMount() {
67 if (this.state.jwt.auth_token.length > 0) {
68 this.fetchUser();
69 }
70 }
71
72 setAuthToken = (authToken: string): Promise<void> => {
73 this.token.setToken(authToken);
74 this.setState({ api: new Api(new Token()) });
75 return this.fetchUser();
76 };
77
78 fetchUser = async () => {
79 await this.state.api.UserService.status()
80 .then(userInfo => {
81 const { categories } = userInfo;
82 this.setState({ userInfo, categories });
83 })
84 .catch(err => this.logout());
85 };
86
87 logout = () => {
88 this.token.revokeToken();
89 this.setState({
90 userInfo: null,
91 jwt: { decoded: null, auth_token: '' }
92 }, () => window.location.assign('/'));
93 };
94
95 render() {
96 const { jwt, userInfo } = this.state;
97 const isBooting = jwt.auth_token.length > 0 && userInfo == null;
98 const appState: AppState = { ...this.state, isBooting };
99
100 return (
101 <AppContext.Provider value={appState}>
102 {this.props.children}
103 </AppContext.Provider>

Callers

nothing calls this directly

Calls 2

setTokenMethod · 0.80
revokeTokenMethod · 0.80

Tested by

no test coverage detected