Przeglądaj źródła

jwt and login works

master
Daniel 2 lat temu
rodzic
commit
5cbee67329
9 zmienionych plików z 65 dodań i 55 usunięć
  1. +1
    -0
      matsen-tool/src/app/_helpers/jwt.interceptor.ts
  2. +2
    -2
      matsen-tool/src/app/_services/account.service.ts
  3. +0
    -8
      matsen-tool/src/app/app.component.html
  4. +8
    -17
      matsen-tool/src/app/app.component.ts
  5. +20
    -18
      matsen-tool/src/app/app.module.ts
  6. +7
    -0
      matsen-tool/src/app/home/home.component.html
  7. +19
    -3
      matsen-tool/src/app/home/home.component.ts
  8. +6
    -5
      matsen-tool/src/app/users/list.component.ts
  9. +2
    -2
      matsen-tool/src/environments/environment.ts

+ 1
- 0
matsen-tool/src/app/_helpers/jwt.interceptor.ts Wyświetl plik

@@ -10,6 +10,7 @@ export class JwtInterceptor implements HttpInterceptor {
constructor(private accountService: AccountService) { } constructor(private accountService: AccountService) { }


intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log('intercept');
// add auth header with jwt if user is logged in and request is to the api url // add auth header with jwt if user is logged in and request is to the api url
const user = this.accountService.userValue; const user = this.accountService.userValue;
const isLoggedIn = user && user.token; const isLoggedIn = user && user.token;


+ 2
- 2
matsen-tool/src/app/_services/account.service.ts Wyświetl plik

@@ -24,8 +24,8 @@ export class AccountService {
return this.userSubject.value; return this.userSubject.value;
} }


login(username: string, password: string) {
return this.http.post<User>(`${environment.apiUrl}/login`, { username, password })
login(email: string, password: string) {
return this.http.post<User>(`${environment.basePath}/auth`, { email, password })
.pipe(map(user => { .pipe(map(user => {
// store user details and jwt token in local storage to keep user logged in between page refreshes // store user details and jwt token in local storage to keep user logged in between page refreshes
localStorage.setItem('user', JSON.stringify(user)); localStorage.setItem('user', JSON.stringify(user));


+ 0
- 8
matsen-tool/src/app/app.component.html Wyświetl plik

@@ -7,14 +7,6 @@
</div> </div>
</nav> </nav>


<!--<ul>-->
<!-- <li>Posts</li>-->
<!-- <li *ngFor="let post of posts">-->
<!-- <h2>{{post.id}} - {{post.owner}}</h2>-->
<!-- <p>{{post.message}}</p>-->
<!-- </li>-->
<!--</ul>-->

<!-- main app container --> <!-- main app container -->
<div class="app-container" [ngClass]="{ 'bg-light': user }"> <div class="app-container" [ngClass]="{ 'bg-light': user }">
<alert></alert> <alert></alert>


+ 8
- 17
matsen-tool/src/app/app.component.ts Wyświetl plik

@@ -1,36 +1,27 @@
import {Component, OnInit} from '@angular/core'; import {Component, OnInit} from '@angular/core';


import { AccountService } from './_services';
import { User } from './_models';
import {AccountService} from './_services';
import {User} from './_models';
import {Subscription} from "rxjs"; import {Subscription} from "rxjs";
import {PostJsonld, PostService} from "@app/core/api/v1"; import {PostJsonld, PostService} from "@app/core/api/v1";


@Component({ @Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrl: 'app.component.scss'
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrl: 'app.component.scss'
}) })
export class AppComponent implements OnInit { export class AppComponent implements OnInit {
user?: User | null; user?: User | null;
protected postSub: Subscription;
protected posts: Array<PostJsonld>;




constructor(private postService: PostService, private accountService: AccountService) { constructor(private postService: PostService, private accountService: AccountService) {
this.accountService.user.subscribe(x => this.user = x); this.accountService.user.subscribe(x => this.user = x);
this.postSub = new Subscription();
this.posts = [];
} }
ngOnInit(): void {
this.postSub = this.postService.postsGetCollection().subscribe(
data => {


this.posts = data["hydra:member"];
console.log(data);
}
);
ngOnInit(): void {

}


}
logout() { logout() {
this.accountService.logout(); this.accountService.logout();
} }


+ 20
- 18
matsen-tool/src/app/app.module.ts Wyświetl plik

@@ -1,26 +1,27 @@
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { ReactiveFormsModule } from '@angular/forms';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {ReactiveFormsModule} from '@angular/forms';
import {HttpClientModule, HTTP_INTERCEPTORS} from '@angular/common/http';


// used to create fake backend // used to create fake backend
import { fakeBackendProvider } from './_helpers';
import {fakeBackendProvider} from './_helpers';


import { AppRoutingModule } from './app-routing.module';
import { JwtInterceptor, ErrorInterceptor } from './_helpers';
import { AppComponent } from './app.component';
import { AlertComponent } from './_components';
import { HomeComponent } from './home';
import {AppRoutingModule} from './app-routing.module';
import {JwtInterceptor, ErrorInterceptor} from './_helpers';
import {AppComponent} from './app.component';
import {AlertComponent} from './_components';
import {HomeComponent} from './home';
import {NgbModule} from "@ng-bootstrap/ng-bootstrap"; import {NgbModule} from "@ng-bootstrap/ng-bootstrap";
import {ApiModule, Configuration, ConfigurationParameters} from "@app/core/api/v1"; import {ApiModule, Configuration, ConfigurationParameters} from "@app/core/api/v1";
import {environment} from "@environments/environment"; import {environment} from "@environments/environment";
import {MatCardModule} from "@angular/material/card"; import {MatCardModule} from "@angular/material/card";


export function apiConfigFactory(): Configuration { export function apiConfigFactory(): Configuration {
const params: ConfigurationParameters = {
basePath: environment.basePath,
};
return new Configuration(params);
const params: ConfigurationParameters = {
basePath: environment.basePath,
withCredentials: false
};
return new Configuration(params);
} }


@NgModule({ @NgModule({
@@ -31,7 +32,7 @@ export function apiConfigFactory(): Configuration {
HttpClientModule, HttpClientModule,
NgbModule, NgbModule,
AppRoutingModule, AppRoutingModule,
MatCardModule
MatCardModule
], ],
declarations: [ declarations: [
AppComponent, AppComponent,
@@ -39,12 +40,13 @@ export function apiConfigFactory(): Configuration {
HomeComponent HomeComponent
], ],
providers: [ providers: [
{ provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true },
{ provide: HTTP_INTERCEPTORS, useClass: ErrorInterceptor, multi: true },
{provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true},
{provide: HTTP_INTERCEPTORS, useClass: ErrorInterceptor, multi: true},


// provider used to create fake backend // provider used to create fake backend
fakeBackendProvider fakeBackendProvider
], ],
bootstrap: [AppComponent] bootstrap: [AppComponent]
}) })
export class AppModule { };
export class AppModule {
};

+ 7
- 0
matsen-tool/src/app/home/home.component.html Wyświetl plik

@@ -58,3 +58,10 @@
</div> </div>
</div> </div>
</div> </div>
<ul>
<li>Posts</li>
<li *ngFor="let post of posts">
<h2>{{post.id}} - {{post.owner}}</h2>
<p>{{post.message}}</p>
</li>
</ul>

+ 19
- 3
matsen-tool/src/app/home/home.component.ts Wyświetl plik

@@ -1,17 +1,33 @@
import { Component } from '@angular/core';
import {Component, OnInit} from '@angular/core';


import { User } from '@app/_models'; import { User } from '@app/_models';
import { AccountService } from '@app/_services'; import { AccountService } from '@app/_services';
import {MatCardModule} from "@angular/material/card"; import {MatCardModule} from "@angular/material/card";
import {Subscription} from "rxjs";
import {PostJsonld, PostService} from "@app/core/api/v1";


@Component({ @Component({
templateUrl: 'home.component.html', templateUrl: 'home.component.html',
styleUrl: 'home.component.scss' styleUrl: 'home.component.scss'
}) })
export class HomeComponent {
export class HomeComponent implements OnInit{
user: User | null; user: User | null;
protected postSub: Subscription;
protected posts: Array<PostJsonld>;


constructor(private accountService: AccountService) {
constructor(private accountService: AccountService, private postService: PostService) {
this.user = this.accountService.userValue; this.user = this.accountService.userValue;
// this.accountService.user.subscribe(x => this.user = x);
this.postSub = new Subscription();
this.posts = [];
}

ngOnInit(): void {
this.postSub = this.postService.postsGetCollection().subscribe(
data => {
this.posts = data["hydra:member"];
}
);

} }
} }

+ 6
- 5
matsen-tool/src/app/users/list.component.ts Wyświetl plik

@@ -12,16 +12,17 @@ export class ListComponent implements OnInit {
protected users: Array<UserJsonld>; protected users: Array<UserJsonld>;


constructor(private userService: UserService) { constructor(private userService: UserService) {
this.usersSub = new Subscription();
this.users = [];
this.usersSub = new Subscription();
this.users = [];
console.log('sdaaadsds');
} }


ngOnInit() { ngOnInit() {
this.usersSub = this.userService.usersGetCollection().subscribe( this.usersSub = this.userService.usersGetCollection().subscribe(
data => { data => {
this.users = data["hydra:member"];
console.log(data);
console.log('sdaaadsds2332232332');
this.users = data["hydra:member"];
console.log(data);
} }
); );
// this.accountService.getAll() // this.accountService.getAll()


+ 2
- 2
matsen-tool/src/environments/environment.ts Wyświetl plik

@@ -4,8 +4,8 @@


export const environment = { export const environment = {
production: false, production: false,
apiUrl: 'https://matsen-tool-be.ddev.site:8443',
basePath: 'https://matsen-tool-be.ddev.site:8443'
basePath: 'https://matsen-tool-be.ddev.site:8443',
apiUrl: 'https://matsen-tool-be.ddev.site:8443/api'
}; };


/* /*


Ładowanie…
Anuluj
Zapisz