Commit 19d6efad authored by Adarsh's avatar Adarsh

comments

parent 9157fc0b
/*! \file
This service file is used to register a user and login purposes, also it sets tokens for each user and they can be retrieved also
through this service. Contains functions to set,get and delete user tokens and also to check login status of a user.
*/
import {EventEmitter, Injectable, Output} from '@angular/core';
import {map} from 'rxjs/operators';
import {HttpClient} from '@angular/common/http';
......@@ -16,6 +21,10 @@ export class ApiService {
constructor(private httpClient: HttpClient) {
}
/*
This function is used to log in the user by posting username and password as it's parameters to the backend php scripts.
*/
public userLogin(username: string, password: string): Observable<any> {
return this.httpClient.post<any>(this.baseUrl + '/login.php', {username, password})
.pipe(map(user => {
......@@ -28,6 +37,10 @@ export class ApiService {
}));
}
/*
This function is used to register the user by posting name, email, username and password as it's parameters to the backend php scripts.
*/
public userReg(name, email, pwd, username): Observable<any> {
return this.httpClient.post<any>(this.baseUrl + '/register.php', {name, email, pwd, username})
.pipe(map(user => {
......@@ -35,18 +48,34 @@ export class ApiService {
}));
}
/*
This function is used to set token for the user to the local storage.
*/
setToken(token: string): void {
localStorage.setItem('sfcode_user_token_2n1289bpxd', token);
}
/*
This function is used to get token for the user from the local storage.
*/
getToken(): string {
return localStorage.getItem('sfcode_user_token_2n1289bpxd');
}
/*
This function is used to delete token from the local storage.
*/
deleteToken(): void {
localStorage.removeItem('sfcode_user_token_2n1289bpxd');
}
/*
This function is used to check the login state of the user by the availability of token in local storage.
*/
isLoggedIn(): boolean {
const userToken = this.getToken();
return userToken != null;
......
/*! \file
This file serves as the routing node of the entire project.
Containing Routes parameters for all possible routes available and wildcard routes redirect to home.
*/
import {NgModule} from '@angular/core';
import {RouterModule, Routes} from '@angular/router';
import {HomeComponent} from './home/home.component';
......
/*! \file
Default App Component File.
*/
import {Component} from '@angular/core';
@Component({
......
/*! \file
Default app module file of Angular containing imports, declarations and providers.
*/
import {BrowserModule} from '@angular/platform-browser';
import {CUSTOM_ELEMENTS_SCHEMA, NgModule} from '@angular/core';
......
/*! \file
This file serves as an authenticator for each user which allows only users to access internal components like home,
files, arena, ide, etc. It is used in routing module for employing such restrictions.
*/
import {Injectable} from '@angular/core';
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router';
import {ApiService} from './api.service';
......@@ -10,6 +15,9 @@ export class AuthGuard implements CanActivate {
constructor(private dataService: ApiService, private router: Router) {
}
/*
This function checks the state of router and calls the boolean function isLogin() to check whether a user is logged in or not.
*/
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
......@@ -18,6 +26,10 @@ export class AuthGuard implements CanActivate {
return this.isLogin(routeUrl);
}
/*
This function calls Api Service to check the login status of the user and redirects the user to appropriate routes according to status.
*/
isLogin(routeUrl: string): boolean {
if (this.dataService.isLoggedIn()) {
return true;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment