first week update

This commit is contained in:
Wowieee4
2026-04-22 21:25:02 +07:00
parent 760bb2de66
commit ff76abbbf6
9 changed files with 347 additions and 143 deletions
@@ -1,35 +1,36 @@
package com.walkguide.controller;
import java.util.Map;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.walkguide.dto.ApiResponse;
import com.walkguide.dto.AuthRequest;
import com.walkguide.dto.AuthResponse;
import com.walkguide.service.AuthService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
@RestController
@RequestMapping("/api/auth") // Ini URL awalnya
@RequestMapping("/api/auth")
@RequiredArgsConstructor
public class AuthController {
private final AuthService authService;
// Ini URL lengkapnya jadi POST http://localhost:8080/api/auth/login
@PostMapping("/login")
public ResponseEntity<?> login(@RequestBody AuthRequest request) {
try {
// Lempar kardus AuthRequest ke Service
AuthResponse response = authService.login(request);
// Kalau sukses, kirim status 200 OK + isinya
return ResponseEntity.ok(response);
} catch (RuntimeException e) {
// Kalau gagal (password salah/email gak ada), kirim status 401 Unauthorized
return ResponseEntity.status(401).body(e.getMessage());
}
public ResponseEntity<ApiResponse<Map<String, String>>> login(@Valid @RequestBody AuthRequest request) {
// Panggil service buat cek password dan bikin token
Map<String, String> tokenData = authService.login(request);
// Bungkus pakai ApiResponse biar sesuai standar Dosen lu!
ApiResponse<Map<String, String>> response = new ApiResponse<>(true, tokenData, "Login berhasil");
return ResponseEntity.ok(response);
}
}
@@ -1,36 +1,48 @@
package com.walkguide.service;
import java.util.HashMap;
import java.util.Map;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import com.walkguide.dto.AuthRequest;
import com.walkguide.dto.AuthResponse;
import com.walkguide.entity.User;
import com.walkguide.repository.UserRepository;
import com.walkguide.security.JwtUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class AuthService {
private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;
private final JwtUtil jwtUtil;
// Wajib panggil BCrypt biar bisa baca password enkripsi dari database
private final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
public AuthResponse login(AuthRequest request) {
// 1. Cari user di database kampus lu berdasarkan email
public Map<String, String> login(AuthRequest request) {
// 1. Cari user di database
User user = userRepository.findByEmail(request.getEmail())
.orElseThrow(() -> new RuntimeException("Email tidak ditemukan!"));
.orElseThrow(() -> new RuntimeException("Email tidak terdaftar"));
// 2. Cocokin password yang diketik sama password yang di-hash di database
// 2. Cocokin password (yang diketik VS yang dienkripsi di database)
if (!passwordEncoder.matches(request.getPassword(), user.getPassword())) {
throw new RuntimeException("Password salah!");
throw new RuntimeException("Password salah");
}
// 3. Kalau email dan password bener, suruh JwtUtil cetak Token
// 3. Kalau bener, bikin Token
// Asumsi JwtUtil lu nerima parameter (email, role). Sesuaikan kalau beda!
String token = jwtUtil.generateToken(user.getEmail(), user.getRole());
// 4. Bungkus token dan rolenya buat dikirim balik ke Flutter
return new AuthResponse(token, user.getRole());
// 4. Balikin ke Controller dalam bentuk Map
Map<String, String> data = new HashMap<>();
data.put("token", token);
data.put("role", user.getRole());
return data;
}
}