Skip to content

Commit a85149b

Browse files
committed
added some features
+ sql file with all the requests needed to create all the database's tables + http file with some requests to test the backend + automatically sign out and redirect to home after any activity if the token has expired
1 parent b67a9e1 commit a85149b

File tree

10 files changed

+91
-10
lines changed

10 files changed

+91
-10
lines changed

angular-11-client/src/app/board-admin/board-admin.component.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Component, OnInit } from '@angular/core';
2+
import { TokenStorageService } from '../_services/token-storage.service';
23
import { UserService } from '../_services/user.service';
34

45
@Component({
@@ -9,7 +10,7 @@ import { UserService } from '../_services/user.service';
910
export 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
}

angular-11-client/src/app/board-moderator/board-moderator.component.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Component, OnInit } from '@angular/core';
2+
import { TokenStorageService } from '../_services/token-storage.service';
23
import { UserService } from '../_services/user.service';
34

45
@Component({
@@ -9,7 +10,7 @@ import { UserService } from '../_services/user.service';
910
export 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
}

angular-11-client/src/app/board-user/board-user.component.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Component, OnInit } from '@angular/core';
2+
import { TokenStorageService } from '../_services/token-storage.service';
23
import { UserService } from '../_services/user.service';
34

45
@Component({
@@ -9,7 +10,7 @@ import { UserService } from '../_services/user.service';
910
export 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
}
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Component, OnInit } from '@angular/core';
22
import { 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';
910
export 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
}
66.1 KB
Binary file not shown.

angular-11-client/src/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
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"

spring-boot-server/src/main/java/com/bezkoder/springjwt/repository/RoleRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
import com.bezkoder.springjwt.models.Role;
1010

1111
@Repository
12-
public interface RoleRepository extends JpaRepository<Role, Long> {
12+
public interface RoleRepository extends JpaRepository<Role, Integer> {
1313
Optional<Role> findByName(ERole name);
1414
}

spring-boot-server/src/main/resources/application.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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
22
spring.datasource.username= root
3-
spring.datasource.password= 123456
3+
spring.datasource.password= 472000
44

55
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5InnoDBDialect
66
spring.jpa.hibernate.ddl-auto= update
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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

0 commit comments

Comments
 (0)