{"size": 1716, "ext": "js", "max_stars_repo_path": "app/services/auth.service.js", "max_stars_repo_name": "hraynaud/rad-drawer-test", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "app/services/auth.service.js", "max_issues_repo_name": "hraynaud/rad-drawer-test", "max_issues_repo_issues_event_min_datetime": "2021-03-10T14:33:17.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-11T10:27:30.000Z", "max_forks_repo_path": "app/services/auth.service.js", "max_forks_repo_name": "hraynaud/rad-drawer-test", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 23.8333333333, "max_line_length": 94, "alphanum_fraction": 0.6282051282}
import nodeify from "nativescript-nodeify";
import jwt from "jsonwebtoken";
import { apiService } from "./api.service";
import { SESSION_USER_KEY, SESSION_AUTH_KEY } from "./constants";
import { store } from "../store";
import * as utils from "~/shared/utils";

const appSettings = require("application-settings");

export const authService = {
    login,
    logout,
    register,
    currentUser,
    isLoggedIn
};

function login(email, password) {
    return apiService
        .post("/login", { email, password })
        .then(handleLogin)
        .catch(error => {
            throw new Error(error);
        });
}

function register(email, password, firstName, lastName) {
    return apiService
        .post("/register", { email, password, firstName, lastName })
        .then(handleRegistration)
        .catch(error => {
            throw new Error(error);
        });
}

function logout() {
    store.dispatch("logout");
}
function currentUser() {
    if (appSettings.getString(SESSION_USER_KEY)) {
        return JSON.parse(appSettings.getString(SESSION_USER_KEY));
    } else {
        return null;
    }
}

function handleRegistration(data) {
    handleLogin(data);
}

function handleLogin(data) {
    if (data && data.error) {
        throw new Error(data.error);
    }

    if (data && data.jwt) {
        signIn(data.jwt);
        utils.enableDrawer();
    }
}

function isLoggedIn() {
    return store.getters.isLoggedIn;
}

function signIn(token) {
    //pass the decoded jwt into IIFE then destructure and set user to extreact name and email.
    // practicing my es6-fu
    let user = (({ email, name }) => ({ email, name }))(jwt.decode(token));
    store.dispatch("login", { token, user });
}
