Skip to content

Instantly share code, notes, and snippets.

View dasmido's full-sized avatar
🐞
Let's Code Today

Mohammed A. dasmido

🐞
Let's Code Today
View GitHub Profile
import { Component, OnInit } from '@angular/core';
import {AuthService} from '../../services/auth.service';
import {Router} from '@angular/router';
@Component({
selector: 'app-profile',
templateUrl: './profile.component.html',
styleUrls: ['./profile.component.css']
})
export class ProfileComponent implements OnInit {
// function for authenticate user
// function to get profile
getProfile(){
let headers = new Headers();
this.loadToken();
headers.append('Authorization', this.authToken);
headers.append('Content-Type', 'application/json');
return this.http.get('http://localhost:9090/users/profile', {headers: headers}).map(res => res.json());
import { Component, OnInit } from '@angular/core';
import {AuthService} from '../../services/auth.service';
import {Router} from '@angular/router';
import {FlashMessagesService} from 'angular2-flash-messages';
@Component({
selector: 'app-navbar',
templateUrl: './navbar.component.html',
styleUrls: ['./navbar.component.css']
})
// function to register user
// function for authenticate user
// function to store user data (token)
storeUserData(token, user){
localStorage.setItem('id_token', token);
localStorage.setItem('user', JSON.stringify(user));
onLoginSubmit(){
//console.log(this.username);
const user = {
username: this.username,
password: this.password
}
this.authService.authenticateUser(user).subscribe(data => {
//console.log(data);
if(data.success){
// register user function
// function for authenticate user
authenticateUser(user){
let headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http.post('http://localhost:9090/users/authenticate', user, {headers: headers}).map(res => res.json());
import { Component, OnInit } from '@angular/core';
import {AuthService} from '../../services/auth.service';
import {Router} from '@angular/router';
import {FlashMessagesService} from 'angular2-flash-messages';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
<h2 class="page-header">Login</h2>
<form (submit) = "onLoginSubmit()">
<div class="form-group">
<label>Username</label>
<input type="text" class="form-control" [(ngModel)] = "username" name="username">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" [(ngModel)] = "password" name="password" >
import {AuthService} from '../../services/auth.service';
import {Router} from '@angular/router';
onRegisterSubmit(){
// Required Fields
// Required Fields for email
// Register user
this.authService.registerUser(user).subscribe(data => {
import { Injectable } from '@angular/core';
import {Http, Headers} from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class AuthService {
authToken: any;