Guest User

Untitled

a guest
Nov 20th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. const express = require('express');
  2. const cookieParser = require('cookie-parser');
  3. const bodyParser = require('body-parser');
  4. const path = require('path');
  5. const http = require('http');
  6. const csurf = require('csurf');
  7.  
  8. const app = express();
  9.  
  10. const csrfProtection = csurf({
  11. cookie: {
  12. key: 'XSRF-TOKEN',
  13. path: '/'
  14. }
  15. });
  16.  
  17. // Parser
  18. app.use(cookieParser());
  19. app.use(bodyParser.urlencoded({
  20. extended: false
  21. }));
  22. // Angular DIST output folder
  23. app.use(express.static(path.join(__dirname, 'dist')));
  24.  
  25. // API location
  26. app.use('/api', csrfProtection, bodyParser.json(), api);
  27.  
  28. // Send all other requests to the Angular app
  29. app.get('*', csrfProtection, (req, res) => {
  30. const cookie = req.csrfToken();
  31. console.log("COOKIE", cookie);
  32. res.cookie('XSRF-TOKEN', cookie);
  33. res.sendFile(path.join(__dirname, 'dist/index.html'));
  34. });
  35.  
  36. // Set port
  37. const port = process.env.PORT || 3000;
  38. app.set('port', port);
  39.  
  40. const server = http.createServer(app);
  41. server.listen(port, () => console.log(`Running on localhost:${port}`));
  42.  
  43. import { Injectable } from '@angular/core';
  44. import { Http, Response, Headers } from '@angular/http';
  45. import 'rxjs/add/operator/toPromise';
  46. import 'rxjs/add/operator/map';
  47.  
  48. import { AppService } from '../../shared/services';
  49.  
  50. @Injectable()
  51. export class LoginService {
  52.  
  53. constructor(private http: Http) { }
  54.  
  55. login(cred: any): Promise<Response> {
  56. const HEADERS = new Headers();
  57. const csrfCookie = AppService.getCookie('XSRF-TOKEN');
  58. console.log(csrfCookie);
  59.  
  60. if (csrfCookie) {
  61. HEADERS.append('csrf-token', csrfCookie);
  62. }
  63. console.log(HEADERS);
  64.  
  65. return this.http.post('/api/login', JSON.stringify(cred), { headers: HEADERS })
  66. .map(res => res.json()).toPromise();
  67. }
  68. }
Add Comment
Please, Sign In to add comment