UserServices.js

69 lines | 1.587 kB Blame History Raw Download

class UserServices {

    constructor() {

        this.URL_Auth = "/User/Auth";
        this.URL_UserInfo = "/User/UserInfo"

        this.AuthCoockieName = "AuthToken";

    }



    AuthAsync(login, password) {

        return fetch(
            this.URL_Auth,
            {
                method: "Post",
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({               
                    Login: login,
                    Password: password
                })
            });
    }

    GetUserInfoAsync() {
        return fetch(
            this.URL_UserInfo,            
            {
                method: "Post",
                credentials: 'include'
            });
    }

    //Возвращает true если пользователь авторизован
    IsAuth() {
        let token = this.GetTocken();
        return token != "";
    }

    _getCookie(name) {
        let matches = document.cookie.match(new RegExp(
            "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
        ));
        return matches ? decodeURIComponent(matches[1]) : "";
    }

    //Токен авторизации
    GetTocken() {
        return this._getCookie(this.AuthCoockieName);
    }
    //задать токен
    SetTocken(val) {
        document.cookie = this.AuthCoockieName + "=" + val;
    }

    //Возвращает true если пользователь авторизован
    IsAuth() {
        let token = this.GetTocken();
        return token != "";
    }

}