Skip to content
Snippets Groups Projects
Select Git revision
  • a95a3c7b095e196f897cd2d22c83eccb9af1a1c0
  • main default protected
  • jw_sonar
  • v6.0.0 protected
  • interactive-mode-preference
  • bedran_exercise-list
  • add_route_user
  • Jw_sonar_backup
  • exercise_list_filter
  • assignment_filter
  • add_route_assignments
  • move-to-esm-only
  • 6.0.0-dev
  • Pre-alpha
  • 5.0.0
  • Latest
  • 4.2.0
  • 4.1.1
  • 4.1.0
  • 4.0.1
  • 4.0.0
  • 3.5.0
  • 3.4.2
  • 3.4.1
  • 3.3.0
  • 3.2.3
  • 3.2.2
  • 3.2.0
  • 3.1.2
  • 3.1.1
  • 3.1.0
  • 3.0.1
32 results

SessionManager.ts

Blame
  • SessionManager.ts 2.14 KiB
    import * as jwt              from 'jsonwebtoken';
    import User                  from '../models/User';
    import LocalConfig           from '../Config/LocalConfig/LocalConfig';
    import LocalConfigKeys       from '../Config/LocalConfig/LocalConfigKeys';
    import axios, { AxiosError } from 'axios';
    import logger                from '../shared/logging/WinstonLogger';
    import HttpManager           from './HttpManager';
    
    
    class SessionManager {
        private _token: string | null = null;
    
        public profile: User = new User();
    
        constructor() { }
    
        private static _instance: SessionManager;
    
        public static get instance(): SessionManager {
            if ( !SessionManager._instance ) {
                SessionManager._instance = new SessionManager();
            }
    
            return SessionManager._instance;
        }
    
        get isLogged(): boolean {
            return this._token !== null;
        }
    
        get token(): string {
            return this._token || '';
        }
    
        set token(token: string) {
            this._token = token;
    
            const payload = jwt.decode(token);
    
            if ( payload && typeof payload === 'object' && payload.profile ) {
                this.profile = User.createFromJson(payload.profile);
            }
    
            LocalConfig.updateConfig(LocalConfigKeys.API_TOKEN, token);
        }
    
        async login(user: string, password: string) {
            try {
                const response = await axios.post(HttpManager.LOGIN_URL, {
                    user    : user,
                    password: password
                }, {
                                                      headers: {
                                                          'Content-Type': 'multipart/form-data'
                                                      }
                                                  });
    
                logger.info('Login successful');
            } catch ( error ) {
                if ( error instanceof AxiosError ) {
                    if ( error.response && error.response.status === 404 ) {
                        logger.error(`User not found or password incorrect`);
                    }
                } else {
                    logger.error(`Login error: ${ error }`);
                }
            }
        }
    }
    
    
    export default SessionManager.instance;