Guest User

Untitled

a guest
Jul 10th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. const webpack = require('webpack');
  2. const HtmlWebpackPlugin = require('html-webpack-plugin');
  3.  
  4. module.exports = {
  5. entry: './src/main.ts',
  6. module: {
  7. rules: [
  8. {
  9. test: /.ts$/,
  10. use: ['ts-loader', 'angular2-template-loader'],
  11. exclude: /node_modules/
  12. },
  13. {
  14. test: /.(html|css)$/,
  15. loader: 'raw-loader'
  16. },
  17. ]
  18. },
  19. resolve: {
  20. extensions: ['.ts', '.js']
  21. },
  22. plugins: [
  23. new HtmlWebpackPlugin({
  24. template: './src/index.html',
  25. filename: 'index.html',
  26. inject: 'body'
  27. }),
  28. new webpack.DefinePlugin({
  29. // global app config object
  30. config: JSON.stringify({
  31. apiUrl: 'http://localhost:4000'
  32. })
  33. })
  34. ],
  35. optimization: {
  36. splitChunks: {
  37. chunks: 'all',
  38. },
  39. runtimeChunk: true
  40. },
  41. devServer: {
  42. historyApiFallback: true
  43. }
  44. };
  45.  
  46. import { Injectable } from '@angular/core';
  47. import { HttpClient } from '@angular/common/http';
  48. import { map } from 'rxjs/operators';
  49.  
  50. @Injectable()
  51. export class AuthenticationService {
  52. constructor(private http: HttpClient) { }
  53.  
  54. login(username: string, password: string) {
  55. return this.http.post<any>(`${config.apiUrl}/users/authenticate`, { username: username, password: password })
  56. .pipe(map(user => {
  57. // login successful if there's a jwt token in the response
  58. if (user && user.token) {
  59. // store user details and jwt token in local storage to keep user logged in between page refreshes
  60. localStorage.setItem('currentUser', JSON.stringify(user));
  61. }
  62.  
  63. return user;
  64. }));
  65. }
  66.  
  67. logout() {
  68. // remove user from local storage to log user out
  69. localStorage.removeItem('currentUser');
  70. }
  71. }
  72.  
  73. import { Injectable } from '@angular/core';
  74. import { HttpClient } from '@angular/common/http';
  75.  
  76. import { User } from '../_models/user';
  77.  
  78. @Injectable()
  79. export class UserService {
  80. constructor(private http: HttpClient) { }
  81.  
  82. getAll() {
  83. return this.http.get<User[]>(`${config.apiUrl}/users`);
  84. }
  85.  
  86. getById(id: number) {
  87. return this.http.get(`${config.apiUrl}/users/` + id);
  88. }
  89.  
  90. register(user: User) {
  91. return this.http.post(`${config.apiUrl}/users/register`, user);
  92. }
  93.  
  94. update(user: User) {
  95. return this.http.put(`${config.apiUrl}/users/` + user.id, user);
  96. }
  97.  
  98. delete(id: number) {
  99. return this.http.delete(`${config.apiUrl}/users/` + id);
  100. }
  101. }
  102.  
  103. {
  104. "extends": "../tsconfig.json",
  105. "compilerOptions": {
  106. "outDir": "../out-tsc/app",
  107. "module": "es2015",
  108. "types": [
  109. "webpack-env"
  110. ]
  111. },
  112. "exclude": [
  113. "src/test.ts",
  114. "**/*.spec.ts"
  115. ]
  116. }
  117.  
  118. {
  119. "extends": "../tsconfig.json",
  120. "compilerOptions": {
  121. "outDir": "../out-tsc/spec",
  122. "module": "commonjs",
  123. "types": [
  124. "jasmine",
  125. "node",
  126. "webpack-env"
  127. ]
  128. },
  129. "files": [
  130. "test.ts",
  131. "polyfills.ts"
  132. ],
  133. "include": [
  134. "**/*.spec.ts",
  135. "**/*.d.ts"
  136. ]
  137. }
Add Comment
Please, Sign In to add comment