2017-03-18 1 views
0

토큰을 로컬 저장소에 저장하는 방법을 알고 싶습니다. 등록 할 때 로컬 저장소에 저장해야하는 관련 토큰이 생성되고 있습니다. 그리고 거기에 로그인 페이지를 열면 나는 그것을 체크 아웃해야합니다.로컬 저장소에 API 토큰 저장 ReactJS

내 코드 :

import React, { Component } from 'react'; 
import { Link } from 'react-router' 
class Register extends Component { 
    constructor(props) { 
    super(props); 
    this.state = { 
     inputfirstname: '', 
     inputlastname: '' , 
     inputemail: '' , 
     inputpassword: '' 
    }; 

    this.handleFirstName = this.handleFirstName.bind(this); 
    this.handleLastName = this.handleLastName.bind(this); 
    this.handleSubmit = this.handleSubmit.bind(this); 
    this.handleEmail = this.handleEmail.bind(this); 
    this.handlePassword = this.handlePassword.bind(this); 
    } 
    static contextTypes = { 
    router: React.PropTypes.object.isRequired 
    } 
    handleFirstName(event) { 
    this.setState({inputfirstname: event.target.value}); 
    } 
    handleLastName(event) { 
    this.setState({inputlastname: event.target.value}); 
    } 
    handleEmail(event) { 
    this.setState({inputemail: event.target.value}); 
    } 
    handlePassword(event) { 
    this.setState({inputpassword: event.target.value}); 
    } 
    handleSubmit(event) { 
    fetch("http://api-env.bt2qjip33m.ap-south-1.elasticbeanstalk.com/api/v1/accounts/" , { 
     method: 'post', 
     headers: { 
     'Content-Type': 'application/json', 
     }, 
     body: JSON.stringify({ 
     firstName: this.state.inputfirstname, 
     lastName: this.state.inputlastname, 
     email:this.state.inputemail, 
     password: this.state.inputpassword 
     }) 
    }).then(function(response){ 
     if(response.ok) { 
     console.log(response) 
     return response; 
     } 
     throw new Error('Network response was not ok.'); 
    }).then(function(data) { 
     console.log(data); 
    }).catch(function(error) { 
     console.log('There has been a problem with your fetch operation: ' + error); 
    }); 
    alert('Submitted: ' + " " + this.state.inputfirstname + " " + this.state.inputlastname + " " + this.state.inputemail + " " + this.state.inputpassword); 
    event.preventDefault(); 
    this.context.router.push('/dashboard'); 
    } 
    render() { 
    return (
     <div className="container"> 
     <div className="row justify-content-center"> 
      <div className="col-md-6"> 
      <div className="card mx-2"> 
       <div className="card-block p-2"> 
       <center> 
       <img src="./img/logos.png"/></center> 
       <p className="text-muted">Create your account</p> 
       <div className="input-group mb-1"> 
        <span className="input-group-addon"><i className="icon-user"></i></span> 
        <input type="text" 
        className="form-control" 
        placeholder="First name" 
        value={this.state.inputfirstname} onChange={this.handleFirstName} 
        /> 
       </div> 
       <div className="input-group mb-1"> 
        <span className="input-group-addon"><i className="icon-user"></i></span> 
        <input type="text" 
        className="form-control" 
        placeholder="Last name" 
        value={this.state.inputlastname} onChange={this.handleLastName} 
        /> 
       </div> 
       <div className="input-group mb-1"> 
        <span className="input-group-addon">@</span> 
        <input type="text" 
        className="form-control" 
        placeholder="Email" 
        value={this.state.inputemail} onChange={this.handleEmail} 
        /> 
       </div> 
       <div className="input-group mb-1"> 
        <span className="input-group-addon"><i className="icon-lock"></i></span> 
        <input type="password" 
        className="form-control" 
        placeholder="Password" 
        value={this.state.inputpassword} onChange={this.handlePassword} 
        /> 
       </div> 
       <button type="button" onClick={this.handleSubmit.bind(this)} className="btn btn-block btn-success">Create Account</button> 

       <center><Link to={'/pages/login'} style={{ width: 10 + '%' }} className="nav-link" activeClassName="active"><center>Login</center></Link> 
</center> 
       </div> 
       <div className="card-footer p-2"> 
       <div className="row"> 
        <div className="col-6"> 
        <button className="btn btn-block btn-facebook" type="button"><span>facebook</span></button> 
        </div> 
        <div className="col-6"> 
        <button className="btn btn-block btn-twitter" type="button"><span>twitter</span></button> 
        </div> 
       </div> 
       </div> 
      </div> 
      </div> 
     </div> 
     </div> 
    ); 
    } 
} 
export default Register; 
+0

처럼 반응 코드에서 사용할 [설정하고 로컬 스토리지 얻을] (https://developer.mozilla.org/en-US/docs/Web/API/을 할 수 있습니다 스토리지/로컬 스토리지). –

답변

1

로컬 스토리지는 글로벌 변수입니다. 당신은 당신은 일반 자바 스크립트를 사용할 수 있습니다 단지

localStorage.set('itemName', value) 
localStorage.get('itemName') 
관련 문제