angular-authentication-service
v1.1.8
Published
Authentication Service For Angular 4
Downloads
19
Maintainers
Readme
AngularAuthenticationService
This project was generated with Angular CLI version 1.4.1.
##Install with NPM $ npm install angular-authentication-service
##main.ts
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AuthenticationServiceModule } from './app/app.module';
import { environment } from './environments/environment';
if (environment.production) {
enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AuthenticationServiceModule).catch(err => console.log(err));
##app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { routing } from './app.routing';
import { LocalStorageModule } from 'angular-2-local-storage';
import { HomeComponent } from './home/home.component';
import { HttpIntercept,AuthIntercept,AuthenticationService,GlobalVarsService,LogIn,User } from 'angular-authentication-service';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
@NgModule({
declarations: [
AppComponent,
HomeComponent
],
imports: [
BrowserModule,
HttpClientModule,
routing,
LocalStorageModule.withConfig({
prefix: 'MyApp',
storageType: 'localStorage'
})
],
exports:[],
providers: [
LogIn,
User,
AuthIntercept,
GlobalVarsService,
AuthenticationService,
{
provide: HTTP_INTERCEPTORS,
useClass: HttpIntercept,
multi: true,
}
],
bootstrap: [AppComponent]
})
export class AppModule {
}
##Component Module Usage
import { Component, OnInit } from '@angular/core';
import { Routes, RouterModule, Router } from '@angular/router';
import { Subscription } from 'rxjs/Subscription';
import { AuthenticationService, User, LogIn, GlobalVarsService } from 'angular-authentication-service';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
// Observable class extensions
import 'rxjs/add/observable/of';
// Observable operators
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app';
user: User;
credentials: LogIn;
constructor(private router: Router,private authenticationservice: AuthenticationService) {
this.authenticationservice.AuthURI = '/api/authenticate';
}
ngOnInit(): void {
this.user = new User;
this.credentials = new LogIn;
this.credentials.username = "username";
this.credentials.password = "password";
this.credentials.app = "MyAppName";
this.authenticationservice.login(this.credentials).subscribe(data => {
if (data) {
this.user = data;
localStorage.setItem('token', data.access_token);
} else {
localStorage.setItem('token', '');
}
}, err => {
console.log('error: ', err);
});
}
}
#Server Side ##routes.js
const express = require('express');
const router = express.Router();
var request = require('request');
var http = require('http');
var https = require('https');
var path = require('path');
var fs = require('fs-extra');
var config = require('./config')
var app = null
var protocol = null
var certfolder = path.join(__dirname, 'certificates');
router.post('/authenticate', (req, res) => {
var str = '';
//API listening on port 8000
var options = {
host: 'localhost',
path: '/authenticate',
port: '8000',
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=UTF-8'
}
}
var request = http.request(options, function (response) {
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
var obj = null;
if (response.statusCode === 200) {
obj = JSON.parse(str);
return res.status(200).send(obj);
} else {
return res.status(response.statusCode).send(response.statusMessage);
}
});
});
request.write(JSON.stringify(req.body));
request.end();
});
module.exports = router;
##server.js
var compression = require('compression')
var config = require('./config')
var express = require('express')
var http = require('http')
var https = require('https')
var bodyParser = require('body-parser')
var path = require('path')
const router = express.Router()
const api = require('./routes')
var app = express()
app.use(compression())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(express.static(path.join(__dirname, 'dist')))
http.globalAgent.maxSockets = 100
var server = http.createServer(app)
app.use(express.static(__dirname + '/dist'))
app.use('/api', api);
app.use(function (req, res) {
res.sendFile(__dirname + '/dist/index.html')
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, './dist/index.html'))
});
server.listen(8043, function () {
console.log('Express server listening on port 8043')
console.log('Press CTRL+C to exit.')
})