File tree Expand file tree Collapse file tree 10 files changed +91
-10
lines changed
spring-boot-server/src/main
java/com/bezkoder/springjwt/repository Expand file tree Collapse file tree 10 files changed +91
-10
lines changed Original file line number Diff line number Diff line change 11import { Component , OnInit } from '@angular/core' ;
2+ import { TokenStorageService } from '../_services/token-storage.service' ;
23import { UserService } from '../_services/user.service' ;
34
45@Component ( {
@@ -9,7 +10,7 @@ import { UserService } from '../_services/user.service';
910export class BoardAdminComponent implements OnInit {
1011 content ?: string ;
1112
12- constructor ( private userService : UserService ) { }
13+ constructor ( private userService : UserService , private tokenStorageService : TokenStorageService ) { }
1314
1415 ngOnInit ( ) : void {
1516 this . userService . getAdminBoard ( ) . subscribe (
@@ -18,6 +19,8 @@ export class BoardAdminComponent implements OnInit {
1819 } ,
1920 err => {
2021 this . content = JSON . parse ( err . error ) . message ;
22+ this . tokenStorageService . signOut ( ) ;
23+ window . location . assign ( "/home" ) ;
2124 }
2225 ) ;
2326 }
Original file line number Diff line number Diff line change 11import { Component , OnInit } from '@angular/core' ;
2+ import { TokenStorageService } from '../_services/token-storage.service' ;
23import { UserService } from '../_services/user.service' ;
34
45@Component ( {
@@ -9,7 +10,7 @@ import { UserService } from '../_services/user.service';
910export class BoardModeratorComponent implements OnInit {
1011 content ?: string ;
1112
12- constructor ( private userService : UserService ) { }
13+ constructor ( private userService : UserService , private tokenStorageService : TokenStorageService ) { }
1314
1415 ngOnInit ( ) : void {
1516 this . userService . getModeratorBoard ( ) . subscribe (
@@ -18,6 +19,8 @@ export class BoardModeratorComponent implements OnInit {
1819 } ,
1920 err => {
2021 this . content = JSON . parse ( err . error ) . message ;
22+ this . tokenStorageService . signOut ( ) ;
23+ window . location . assign ( "/home" ) ;
2124 }
2225 ) ;
2326 }
Original file line number Diff line number Diff line change 11import { Component , OnInit } from '@angular/core' ;
2+ import { TokenStorageService } from '../_services/token-storage.service' ;
23import { UserService } from '../_services/user.service' ;
34
45@Component ( {
@@ -9,7 +10,7 @@ import { UserService } from '../_services/user.service';
910export class BoardUserComponent implements OnInit {
1011 content ?: string ;
1112
12- constructor ( private userService : UserService ) { }
13+ constructor ( private userService : UserService , private tokenStorageService : TokenStorageService ) { }
1314
1415 ngOnInit ( ) : void {
1516 this . userService . getUserBoard ( ) . subscribe (
@@ -18,6 +19,8 @@ export class BoardUserComponent implements OnInit {
1819 } ,
1920 err => {
2021 this . content = JSON . parse ( err . error ) . message ;
22+ this . tokenStorageService . signOut ( ) ;
23+ window . location . assign ( "/home" ) ;
2124 }
2225 ) ;
2326 }
Original file line number Diff line number Diff line change 11import { Component , OnInit } from '@angular/core' ;
22import { TokenStorageService } from '../_services/token-storage.service' ;
3+ import { UserService } from '../_services/user.service' ;
34
45@Component ( {
56 selector : 'app-profile' ,
@@ -9,9 +10,18 @@ import { TokenStorageService } from '../_services/token-storage.service';
910export class ProfileComponent implements OnInit {
1011 currentUser : any ;
1112
12- constructor ( private token : TokenStorageService ) { }
13-
13+ constructor ( private token : TokenStorageService , private userService : UserService ) { }
14+ //me
1415 ngOnInit ( ) : void {
15- this . currentUser = this . token . getUser ( ) ;
16+ this . userService . getUserBoard ( ) . subscribe (
17+ data => {
18+ this . currentUser = this . token . getUser ( ) ;
19+ } ,
20+ err => {
21+ this . token . signOut ( ) ;
22+ window . location . assign ( "/home" ) ;
23+ }
24+ ) ;
25+
1626 }
1727}
Original file line number Diff line number Diff line change 55 < title > Angular11JwtAuth</ title >
66 < base href ="/ " />
77 < meta name ="viewport " content ="width=device-width, initial-scale=1 " />
8- < link rel ="icon " type ="image/x-icon " href ="favicon .ico " />
8+ < link rel ="icon " type ="image/x-icon " href ="./assets/security .ico " />
99 < link
1010 rel ="stylesheet "
1111 href ="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css "
Original file line number Diff line number Diff line change 99import com .bezkoder .springjwt .models .Role ;
1010
1111@ Repository
12- public interface RoleRepository extends JpaRepository <Role , Long > {
12+ public interface RoleRepository extends JpaRepository <Role , Integer > {
1313Optional <Role > findByName (ERole name );
1414}
Original file line number Diff line number Diff line change 1- spring.datasource.url = jdbc:mysql://localhost:3306/testdb ?useSSL=false&useJDBCCompliantTimezoneShift=true&serverTimezone=UTC
1+ spring.datasource.url = jdbc:mysql://localhost:3306/cpc ?useSSL=false&useJDBCCompliantTimezoneShift=true&serverTimezone=UTC
22spring.datasource.username = root
3- spring.datasource.password = 123456
3+ spring.datasource.password = 472000
44
55spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
66spring.jpa.hibernate.ddl-auto = update
Original file line number Diff line number Diff line change 1+ drop table user_roles;
2+ drop table roles;
3+ drop table users;
4+
5+ create table users
6+ (
7+ id bigint auto_increment
8+ primary key ,
9+ username varchar (20 ) not null ,
10+ email varchar (50 ) not null ,
11+ password varchar (120 ) not null ,
12+ constraint email
13+ unique (email),
14+ constraint username
15+ unique (username)
16+ );
17+
18+ create table roles
19+ (
20+ id int auto_increment
21+ primary key ,
22+ name varchar (20 ) null
23+ );
24+ insert into roles (name) values (" ROLE_USER" ),(" ROLE_MODERATOR" ),(" ROLE_ADMIN" );
25+
26+ create table user_roles
27+ (
28+ user_id bigint not null ,
29+ role_id int not null ,
30+ primary key (user_id, role_id),
31+ constraint fk_user_role_role
32+ foreign key (role_id) references roles (id),
33+ constraint fk_user_role_user
34+ foreign key (user_id) references users (id)
35+ );
Original file line number Diff line number Diff line change 1+ POST http://localhost:8080/api/auth/signup
2+ Content-Type: application/json
3+
4+ {
5+ "username" : " moderator" ,
6+ "email" : " moderator@gmail.com" ,
7+ "role" : [" mod" ," user" ],
8+ "password" : " password"
9+ }
10+ ###
11+ GET http://localhost:8080/api/test/all
12+
13+ ###
14+ POST http://localhost:8080/api/auth/signin
15+ Content-Type: application/json
16+
17+ {
18+ "username" : " moderator" ,
19+ "password" : " password"
20+ }
21+ ###
22+ GET http://localhost:8080/api/test/mod
23+ Authorization: Bearer replace_with_actual_access_token
24+
25+ ###
26+ GET http://localhost:8080/api/test/admin
27+ Authorization: Bearer replace_with_actual_access_token
You can’t perform that action at this time.
0 commit comments