{"size": 1026, "ext": "js", "max_stars_repo_path": "resources/js/apis/User.js", "max_stars_repo_name": "MdEahiaMondal/real-time-forum", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "resources/js/apis/User.js", "max_issues_repo_name": "MdEahiaMondal/real-time-forum", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "resources/js/apis/User.js", "max_forks_repo_name": "MdEahiaMondal/real-time-forum", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 18.6545454545, "max_line_length": 63, "alphanum_fraction": 0.537037037}

import Token from "../helpers/Token";
import AppStorage from "../helpers/AppStorage";

class User {
    //save user info and token on local storage to user browser
    responseAfterLoggedIn(res){
        const token = res.data.access_token
        const user = res.data.user
       if(Token.isValid(token)){
            AppStorage.store(user, token)
       }
    }

    hasToken(){
        const appToken = AppStorage.getToken()
        if (appToken){
            return  Token.isValid(appToken) ? true : false
        }
        return false
    }

    loggedIn(){
        return this.hasToken()
    }

    logout(){
        AppStorage.clear()
        window.location = '/forum'
    }

    name(){
        if(this.loggedIn()){
            return  AppStorage.getUser()
        }
    }

    id(){
        if(this.loggedIn()){
            const token = Token.payload(AppStorage.getToken())
            return token.sub
        }
    }

    own(id){
        return this.id() === id
    }


}


export default User = new User();

