Skip to content

Instantly share code, notes, and snippets.

View theoomoregbee's full-sized avatar
👀

Theophilus Omoregbee theoomoregbee

👀
View GitHub Profile
@theoomoregbee
theoomoregbee / auth.guard.ts
Last active July 8, 2017 19:41
EVERYTHING YOU NEED TO KNOW ON SECURING YOUR ANGULAR 2+ SPA --> method to be called by canActivate and canActivateChild
/**
* this is method does the checking for us, according to the below process
* 1. check if the user is authenticated, if so check if is time to refresh token the return the observable
* so our guard can resolve it, since the retrieve method is already handling the response with `do` we just map this
* to true default so next view can check and see if the new token is valid or not
*
* 2. if the above from 1 pass through without returning it means it was false all the way
* so we handle it by passing a message and updating the redirectUrl so users can continue where they left of
* after authentication
* @param next
@theoomoregbee
theoomoregbee / auth.guard.ts
Created July 8, 2017 19:44
EVERYTHING YOU NEED TO KNOW ON SECURING YOUR ANGULAR 2+ SPA --> Auth guard completed
import {Injectable} from '@angular/core';
import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanActivateChild, Router} from '@angular/router';
import {Observable} from 'rxjs/Observable';
import {AuthService} from "../providers/auth.service";
import 'rxjs/add/operator/mapTo';
@Injectable()
export class AuthGuard implements CanActivate, CanActivateChild {
@theoomoregbee
theoomoregbee / dashboard.routes.ts
Last active July 8, 2017 19:47
EVERYTHING YOU NEED TO KNOW ON SECURING YOUR ANGULAR 2+ SPA --> Dashboard routes update
import {Routes} from "@angular/router";
import {DashboardComponent} from "./dashboard/dashboard.component";
import {HomeComponent} from "./home/home.component";
import {SettingsComponent} from "./settings/settings.component";
import {AdminComponent} from "./admin/admin.component";
import {AuthGuard} from "../guards/auth.guard";
/**
* Created by theophy on 04/06/2017.
*/
export const dashboardRoutes: Routes = [
@theoomoregbee
theoomoregbee / role.guard.ts
Created July 8, 2017 19:57
EVERYTHING YOU NEED TO KNOW ON SECURING YOUR ANGULAR 2+ SPA --> Role Guard
import {Injectable} from '@angular/core';
import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
import {Observable} from 'rxjs/Observable';
import {UserService} from "../providers/user.service";
@Injectable()
export class RoleGuard implements CanActivate {
constructor(private _userService: UserService) {
}
@theoomoregbee
theoomoregbee / app.component.ts
Created July 8, 2017 20:01
EVERYTHING YOU NEED TO KNOW ON SECURING YOUR ANGULAR 2+ SPA --> User service singleton , getting user
import {Component, OnInit} from '@angular/core';
import {UserService} from "./providers/user.service";
import {JwtHelper} from "./helpers/jwt-helper";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
@theoomoregbee
theoomoregbee / role.guard.ts
Created July 8, 2017 20:07
EVERYTHING YOU NEED TO KNOW ON SECURING YOUR ANGULAR 2+ SPA --> Role guard updated
import {Injectable} from '@angular/core';
import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router} from '@angular/router';
import {Observable} from 'rxjs/Observable';
import {UserService} from "../providers/user.service";
@Injectable()
export class RoleGuard implements CanActivate {
constructor(private _userService: UserService, private _router: Router) {
}
@theoomoregbee
theoomoregbee / User.js
Last active July 8, 2017 20:40
EVERYTHING YOU NEED TO KNOW ON SECURING YOUR ANGULAR 2+ SPA --> Backend User model
/**
* User.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs :: http://sailsjs.org/documentation/concepts/models-and-orm/models
*/
var bcrypt = require("bcryptjs");
module.exports = {
@theoomoregbee
theoomoregbee / userController.js
Created July 9, 2017 00:11
EVERYTHING YOU NEED TO KNOW ON SECURING YOUR ANGULAR 2+ SPA --> Backend User controller
/**
* UserController
*
* @description :: Server-side logic for managing users
* @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers
*/
var jwt = require("jsonwebtoken");
var bcrypt = require("bcryptjs");
@theoomoregbee
theoomoregbee / isAuth.js
Created July 9, 2017 00:14
EVERYTHING YOU NEED TO KNOW ON SECURING YOUR ANGULAR 2+ SPA --> Backend Auth policy
/**
* Created by theophy on 11/06/2017.
*/
var jwt = require("jsonwebtoken");
module.exports = function (req, res, next) {
var bearerToken;
var bearerHeader = req.headers["authorization"];
if (typeof bearerHeader !== 'undefined') {
var bearer = bearerHeader.split(" ");
@theoomoregbee
theoomoregbee / policies.js
Created July 9, 2017 07:24
EVERYTHING YOU NEED TO KNOW ON SECURING YOUR ANGULAR 2+ SPA --> Backend config/policies
/**
* Policy Mappings
* (sails.config.policies)
*
* Policies are simple functions which run **before** your controllers.
* You can apply one or more policies to a given controller, or protect
* its actions individually.
*
* Any policy file (e.g. `api/policies/authenticated.js`) can be accessed
* below by its filename, minus the extension, (e.g. "authenticated")