{"size": 2079, "ext": "js", "max_stars_repo_path": "JS Aplicatinos/08. JS-Applications - Routing - Exercise/Team-Manager-Skeleton/scripts/auth.js", "max_stars_repo_name": "rumenand/Softuni_HomeWork_Tasks", "max_stars_repo_stars_event_min_datetime": "2019-08-25T07:08:16.000Z", "max_stars_repo_stars_event_max_datetime": "2019-08-25T07:08:16.000Z", "max_issues_repo_path": "JS Aplicatinos/08. JS-Applications - Routing - Exercise/Team-Manager-Skeleton/scripts/auth.js", "max_issues_repo_name": "rumenand/HomeWork_Tasks", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "JS Aplicatinos/08. JS-Applications - Routing - Exercise/Team-Manager-Skeleton/scripts/auth.js", "max_forks_repo_name": "rumenand/HomeWork_Tasks", "max_forks_repo_forks_event_min_datetime": "2020-04-14T07:29:27.000Z", "max_forks_repo_forks_event_max_datetime": "2020-04-14T07:29:27.000Z"}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 42.4285714286, "max_line_length": 92, "alphanum_fraction": 0.6094276094}
import {getUserData,applyCommon} from './helpers.js';

export async function loginViewHandler(){
    await applyCommon.call(this);
    this.partials.loginForm = await this.load('./templates/login/loginForm.hbs');
    await this.partial('./templates/login/loginPage.hbs');
    const formCtn = document.querySelector('#login-form');
    formCtn.addEventListener('submit',(e)=>{
        e.preventDefault();
        const chUser = getUserData(formCtn,['username','password']);
        firebase.auth().signInWithEmailAndPassword(chUser.username, chUser.password)
        .then((res)=>{
                firebase.auth().currentUser.getIdToken().then(token=>{
                sessionStorage.setItem('token',token);
                sessionStorage.setItem('username',res.user.email);
                this.redirect('#/home');
            })
        })
        .catch((e) => alert(e));
    })
}
export function logoutHandler(){
    sessionStorage.clear();
    firebase.auth().signOut().then(()=> {
        this.redirect('#/home');
      }).catch((e) => alert(e));
}
export async function registerViewHandler(){
    await applyCommon.call(this);
    this.partials.registerForm = await this.load('./templates/register/registerForm.hbs');
    await this.partial('./templates/register/registerPage.hbs');
    const formCtn = document.querySelector('#register-form');
    formCtn.addEventListener('submit',(e)=>{
        e.preventDefault();
        const chUser = getUserData(formCtn,['username','password','repeatPassword']);
        if (chUser.password === chUser.repeatPassword){
            firebase.auth().createUserWithEmailAndPassword(chUser.username, chUser.password)
            .then((user)=>{
                firebase.auth().currentUser.getIdToken().then(token=>{
                    sessionStorage.setItem('token',token);
                    sessionStorage.setItem('username',user.user.email);
                    this.redirect('#/home');
                })
            })
            .catch((e) => alert(e.message));
        }
        else alert('Passwordn did not match!');
    })
}