login(loginId, password) {
return axios
.post(API_URL + "login", {
loginId,
password
})
.then(response => {
console.log(response.data);
if (response.data) {
localStorage.setItem("token", JSON.stringify(response.data));
localStorage.setItem("user", this.getUser(loginId));
console.log(localstorage.getItem("user");
}
console.log(response.data);
return response.data;
});
}
getUser(loginId){
return axios
.get(API_URL+"user/search/"+loginId,{
headers: { Authorization: `Bearer ${authHeader()} ` },
});
getCurrentUser() {
return (JSON.parse(localStorage.getItem('user')));
}
}
class ViewMytweetComponent extends Component {
constructor(props) {
super(props)
this.onChangeReply = this.onChangeReply.bind(this);
this.state = {
Tweet: [],
reply: "",
user: AuthService.getCurrentUser()
}
this.deleteTweet = this.deleteTweet.bind(this);
}
componentDidMount() {
const { user } = this.state;
console.log(user);
var userId = user.loginId;
TweetDataService.getMyTweet(userId).then((res) => {
this.setState({ Tweet: res.data });
// console.log(this.state.Tweet);
});
}
}
In the login
method I call the getUser
method and store its return value to localStorage
with the key user. The getCurrentUser
method is used to return the stored user-item from the localStorage object.
Requesting the previously stored user in the componentDidMount
method however fails. Logging the user object to the console produces:
[object Promise]
.
Does anyone know how to solve this?