Skip to content

Commit

Permalink
login and signup implement
Browse files Browse the repository at this point in the history
  • Loading branch information
charakamihiranga committed Aug 28, 2024
1 parent 4687b19 commit f5c1460
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.CrossOrigin;

@SpringBootApplication
public class JwtApplication {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import static org.springframework.security.config.Customizer.withDefaults;

@EnableWebSecurity
@Configuration
@EnableMethodSecurity(prePostEnabled = true)
Expand All @@ -42,6 +44,7 @@ public AuthenticationManager authenticationManager (
protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http
.csrf(AbstractHttpConfigurer::disable)
.cors(withDefaults())
.authorizeHttpRequests(auth -> auth
.requestMatchers("/api/v1/auth/authenticate",
"/api/v1/auth/register").permitAll()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package lk.ijse.gdse.springboot.jwt.controller;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api/v1/blog")
@CrossOrigin
public class BlogController {
@GetMapping("/newMethod")
public String newMethod() { return "newMethod called";}
Expand Down
56 changes: 56 additions & 0 deletions JWT/JWT-Frontend/dashboard.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<script src="js/jquery-3.7.1.min.js"></script>
<title>Dashboard</title>
<style>
body {
background-color: #f8f9fa;
font-family: Arial, sans-serif;
}
.container {
margin-top: 20px;
}
.dashboard-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 20px;
background-color: #ffffff;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.dashboard-header h1 {
margin: 0;
color: #333333;
}
.btn-logout:hover {
background-color: #007bff;
}
.btn-logout {
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
padding: 10px 20px;
cursor: pointer;
transition: background-color 0.3s;
}
</style>
</head>
<body>
<section class="container">
<div class="dashboard-header">
<h1>Dashboard</h1>
<button type="submit" class="btn-logout" onclick="logOut()">Logout</button>
</div>
<p id="resp"></p>
</section>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
<script src="/js/dashboard.js"></script>
</body>
</html>
39 changes: 37 additions & 2 deletions JWT/JWT-Frontend/js/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ function userRegistration() {
// error handling
success: function(response) {
console.log(response);
localStorage.setItem('token', response.data.token)
clearFields();
alert('User registered successfully');
},
error: function(error) {
console.log(error);
alert('Error registering user');
alert(error.responseJSON.message);
}
});

Expand All @@ -42,3 +42,38 @@ function clearFields() {
$('#email').val('');
$('#password').val('');
}

function userLogin() {
event.preventDefault();

//define variables
var email = $('#email').val();
var password = $('#password').val();

console.log({
email: email,
password: password
});

//create ajax request
$.ajax({
url: 'http://localhost:8080/api/v1/auth/authenticate',
method: 'POST',
contentType: "application/json",
"data":JSON.stringify({
email: email,
password: password
}),
// error handling
success: function(response) {
console.log(response);
localStorage.setItem('token', response.data.token)
clearFields();
window.location.href = 'dashboard.html';
},
error: function(error) {
console.log(error);
alert(error.responseJSON.message);
}
});
}
29 changes: 29 additions & 0 deletions JWT/JWT-Frontend/js/dashboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
$(document).ready(function() {
let token = localStorage.getItem('token');
if (token) {
//
$.ajax({
url: "http://localhost:8080/api/v1/blog/newMethod",
method: "GET",
contentType: "application/json",
headers: {
"Authorization": `Bearer ${token}`
},
success:function (response) {
console.log(response);
$('<p>'+response+'</p>').appendTo('.container')
},
error : function (error) {
console.log(error);
}
})
}else {
window.location.href = 'signin.html';
}
});

function logOut() {
localStorage.removeItem('token');
window.location.href = 'signin.html';
}

6 changes: 3 additions & 3 deletions JWT/JWT-Frontend/signin.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

<body>
<div class="container">
<h2 class="text-center mb-4">Sign In</h2>
<h2 class="text-center mb-4">Login</h2>
<form>
<div class="mb-3">
<label for="email" class="form-label">Email</label>
Expand All @@ -38,14 +38,14 @@ <h2 class="text-center mb-4">Sign In</h2>
<input type="password" class="form-control" id="password" required>
</div>
<div class="d-grid gap-2">
<button type="submit" class="btn btn-primary w-100">Sign In</button>
<button class="btn btn-primary w-100" onclick="userLogin()">Sign In</button>
</div>
<p class="text-center mt-3">Don't have an account? <a href="signup.html">Sign Up</a></p>
</form>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
<!--<script src="/js/auth.js"></script>-->
<script src="/js/auth.js"></script>
</body>

</html>

0 comments on commit f5c1460

Please sign in to comment.