Skip to content
This repository was archived by the owner on May 21, 2026. It is now read-only.

Commit 4631597

Browse files
Merge branch 'dev' into pr/19
2 parents 03ccc55 + 40c7f80 commit 4631597

5 files changed

Lines changed: 65 additions & 10 deletions

File tree

src/main/java/com/espacogeek/geek/config/CorsConfig.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,32 @@
44
import org.springframework.lang.NonNull;
55
import org.springframework.web.servlet.config.annotation.CorsRegistry;
66
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
7+
import org.springframework.beans.factory.annotation.Value;
8+
9+
import java.util.Arrays;
710

811
@Configuration
912
public class CorsConfig implements WebMvcConfigurer {
1013

14+
@Value("${spring.mvc.cors.allowed-origins:http://localhost:3000}")
15+
private String allowedOrigins;
16+
17+
@Value("${security.jwt.expiration-ms:604800000}")
18+
private long expirationMs;
19+
1120
@Override
1221
public void addCorsMappings(@NonNull CorsRegistry registry) {
22+
String[] origins = Arrays.stream(allowedOrigins.split(","))
23+
.map(String::trim)
24+
.filter(s -> !s.isEmpty())
25+
.toArray(String[]::new);
26+
1327
registry.addMapping("/**")
14-
.allowedOriginPatterns("*")
15-
.allowedMethods("POST")
28+
.allowedOrigins(origins)
29+
.allowedMethods("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS")
1630
.allowedHeaders("*")
17-
.allowCredentials(true);
31+
.exposedHeaders("Authorization", "Content-Type")
32+
.allowCredentials(true)
33+
.maxAge(expirationMs / 1000); // maxAge is in seconds
1834
}
1935
}

src/main/java/com/espacogeek/geek/config/SecurityConfig.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@ public SecurityFilterChain configure(HttpSecurity http) throws Exception {
4949
authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
5050
var authenticationManager = authenticationManagerBuilder.build();
5151

52-
return http.csrf(csrf -> csrf.disable())
52+
return http
53+
.cors(withDefaults())
54+
.csrf(csrf -> csrf.disable())
5355
.authorizeHttpRequests(auth -> {
5456
auth.requestMatchers("/api", "/graphiql/**").permitAll();
57+
auth.requestMatchers(org.springframework.http.HttpMethod.OPTIONS, "/**").permitAll();
5558
auth.anyRequest().authenticated();
5659
})
5760
.sessionManagement(

src/main/java/com/espacogeek/geek/controllers/UserController.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,36 @@ public List<UserModel> findUser(@Argument Integer id, @Argument String username,
3333
return userService.findByIdOrUsernameContainsOrEmail(id, username, email);
3434
}
3535

36+
@QueryMapping(name = "logout")
37+
@PreAuthorize("hasRole('user')")
38+
public String doLogoutUser(Authentication authentication) {
39+
Integer userId = Utils.getUserID(authentication);
40+
41+
UserModel user = userService.findById(userId).get();
42+
user.setJwtToken(null);
43+
userService.save(user);
44+
45+
return HttpStatus.OK.toString();
46+
}
47+
48+
@QueryMapping(name = "isLogged")
49+
@PreAuthorize("hasRole('user')")
50+
public String isUserLogged(Authentication authentication) {
51+
Integer userId = Utils.getUserID(authentication);
52+
53+
UserModel user = userService.findById(userId).get();
54+
String token = user.getJwtToken();
55+
if (token != null) {
56+
try {
57+
if (jwtConfig.isValid(token)) {
58+
return HttpStatus.OK.toString();
59+
}
60+
} catch (Exception ignored) { }
61+
}
62+
63+
throw new GenericException(HttpStatus.UNAUTHORIZED.toString());
64+
}
65+
3666
/**
3767
* Authenticate with email and password and return a JWT token.
3868
*/

src/main/resources/.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
SPRING_DATASOURCE_URL=jdbc:mysql://.../espacogeekdb
22
SPRING_DATASOURCE_USERNAME=root
33
SPRING_DATASOURCE_PASSWORD=root
4+
SPRING_MVC_CORS_ALLOWED_ORIGINS=http://localhost:3000,http://localhost:5173
5+
SECURITY_JWT_ISSUER=espacogeek
6+
SECURITY_JWT_EXPIRATION_MS=604800000
7+
SECURITY_JWT_SECRET=your_jwt_secret_key_here

src/main/resources/graphql/query.graphqls

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,43 @@ type Query {
88
Example: findUser(username: "john")
99
"""
1010
findUser(id: ID, username: String, email: String): [User]
11-
11+
1212
"""
1313
Search for TV series by ID or name.
1414
Example: tvserie(name: "Breaking Bad")
1515
"""
1616
tvserie(id: ID, name: String): MediaPage
17-
17+
1818
"""
1919
Search for games by ID or name with pagination.
2020
Example: game(name: "The Last of Us", page: 0, size: 10)
2121
"""
2222
game(id: ID, name: String, page: Int, size: Int): MediaPage
23-
23+
2424
"""
2525
Search for visual novels by ID or name with pagination.
2626
Example: vn(name: "Steins;Gate", page: 0, size: 10)
2727
"""
2828
vn(id: ID, name: String, page: Int, size: Int): MediaPage
29-
29+
3030
"""
3131
Get detailed media information by ID.
3232
Example: media(id: "123")
3333
"""
3434
media(id: ID): Media
35-
35+
3636
"""
3737
Authenticate and get JWT token.
3838
Example: login(email: "user@example.com", password: "password123")
3939
Returns: JWT token string
4040
"""
4141
login(email: String, password: String): String
42-
42+
4343
"""
4444
Get a random daily quote with artwork.
4545
Example: dailyQuoteArtwork
4646
"""
47+
logout: String
48+
isLogged: String
4749
dailyQuoteArtwork: QuoteArtwork
4850
}

0 commit comments

Comments
 (0)