Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React from 'react';
- const UsuarioContext = React.createContext({
- usuarioLogado: {},
- doLogin: () => { },
- doLogoff: () => { },
- PersistLogin: () => { },
- RestoreUsuario: () => { }
- });
- var cookieName = "HidroUltraApp_";
- var headers = new Headers();
- headers.append('Content-Type', 'application/json');
- var fInit = {
- method: 'POST',
- headers: headers,
- cache: 'default',
- mode: 'same-origin',
- body: {}
- }
- export class UsuarioProvider extends React.Component {
- constructor(props) {
- super(props);
- this.state = {
- usuarioLogado: {},
- doLogin: this.doLogin,
- doLogoff: this.doLogoff,
- PersistLogin: this.PersistLogin,
- RestoreUsuario: this.RestoreUsuario
- };
- }
- doLogin = (usuario, senha, path) => {
- fInit.body = JSON.stringify({ usuario: usuario, senha: senha });
- fetch("/v1/user/login", fInit)
- .then(rest => {
- return rest.json();
- })
- .then(d => {
- this.setState({ usuarioLogado: d });
- console.log(d, "userdata");
- console.log("usuario logado com sucesso!!!");
- })
- .catch(console.log);
- }
- PersistLogin = (user) => { this.localStorage.setItem(cookieName, JSON.stringify(user)); };
- RestoreUsuario = () => {
- this.setState({ usuarioLogado: JSON.parse(localStorage.getItem(cookieName)) });
- }
- doLogoff = () => { this.localStorage.removeItem(cookieName); }
- render() {
- return (
- <UsuarioContext.Provider value={this.state}>
- {this.props.children}
- </UsuarioContext.Provider>
- )
- }
- }
- export const UsuarioConsumer = UsuarioContext.Consumer;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement