Skip to content
Snippets Groups Projects
Commit e62d5664 authored by Szeghy Géza László's avatar Szeghy Géza László
Browse files

backend

parent 22b397b4
No related branches found
No related tags found
No related merge requests found
Showing
with 178 additions and 39 deletions
...@@ -29,12 +29,17 @@ ...@@ -29,12 +29,17 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.h2database</groupId> <groupId>com.h2database</groupId>
<artifactId>h2</artifactId> <artifactId>h2</artifactId>
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
......
package hu.pazmany;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@CrossOrigin(origins = "http://localhost:3000")
@RestController
public class ExampleController {
private final UserService userService;
@Autowired
public ExampleController(UserService userService) {
this.userService = userService;
}
@PostMapping("/api/login")
public String login(@RequestBody UserLoginDto userLoginDto) {
System.out.println(userLoginDto);
return userService.authenticate(userLoginDto.getUsername(), userLoginDto.getPassword());
}
@PostMapping("/api/register")
public void register(@RequestBody UserLoginDto userLoginDto) {
userService.register(userLoginDto.getFullName(), userLoginDto.getUsername(), userLoginDto.getPassword());
}
}
\ No newline at end of file
...@@ -4,10 +4,10 @@ import org.springframework.boot.SpringApplication; ...@@ -4,10 +4,10 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = { "hu.pazmany" }) @SpringBootApplication(scanBasePackages = { "hu.pazmany" })
public class ExampleApplication { public class NoteStore {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(ExampleApplication.class, args); SpringApplication.run(NoteStore.class, args);
} }
} }
package hu.pazmany;
import jakarta.persistence.*;
@Entity
@Table(name = "`user`")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String fullName;
private String username;
private String password; //Ide fog kelleni egy enkriptálás (hajrá)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package hu.pazmany;
public class UserLoginDto {
private String fullName;
private String username;
private String password;
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package hu.pazmany;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(String username);
}
package hu.pazmany;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
private final UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public String authenticate(String username, String password) {
User user = userRepository.findByUsername(username);
if (user != null && user.getPassword().equals(password)) {
return user.getFullName();
}
return "Helytelen felhasználónév vagy jelszó!";
}
public void register(String fullName, String username, String password) {
User user = new User();
user.setFullName(fullName);
user.setUsername(username);
user.setPassword(password);
userRepository.save(user);
}
}
package hu.pazmany.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class ExampleController {
private static final String OKAY_STRING = "OK";
/**
* Provides an OK string
* @return An OK string
*/
@GetMapping("/ok")
public String ok() {
return OKAY_STRING;
}
}
...@@ -7,6 +7,7 @@ spring.datasource.driverClassName=org.h2.Driver ...@@ -7,6 +7,7 @@ spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa spring.datasource.username=sa
spring.datasource.password=sa spring.datasource.password=sa
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.h2.console.enabled=true spring.h2.console.enabled=true
spring.h2.console.path=/h2-console spring.h2.console.path=/h2-console
......
package hu.pazmany;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ExampleApplicationTests {
@Test
void contextLoads() {
}
}
...@@ -27,14 +27,30 @@ import axios from 'axios'; ...@@ -27,14 +27,30 @@ import axios from 'axios';
// }) // })
// } // }
function loginUser(credentials, callback) {
axios.post('http://localhost:8080/api/login', credentials)
.then(response => {
callback(response.data)
})
.catch(error => {
console.log(error);
});
}
const loggedIn = ref(false); const loggedIn = ref(false);
const btnText = ref("Bejelentkezés") const btnText = ref("Bejelentkezés")
const userName = ref("")
function login(){ function login(){
loggedIn.value = !loggedIn.value loggedIn.value = !loggedIn.value
if(loggedIn.value){ if(loggedIn.value){
//Itt lesz a kitalált user (Gipsz Jakab, keksz) aki megpróbál bejelentkezni
loginUser({username: 'gipja', password: 'keksz'}, function(response) {
userName.value = response
})
btnText.value = "Kijelentkezés" btnText.value = "Kijelentkezés"
} else { } else {
userName.value = ""
btnText.value = "Bejelentkezés" btnText.value = "Bejelentkezés"
} }
} }
...@@ -42,6 +58,7 @@ function login(){ ...@@ -42,6 +58,7 @@ function login(){
</script> </script>
<template> <template>
<h1>{{userName}}</h1>
<button class="grow" @click="login">{{btnText}}</button> <button class="grow" @click="login">{{btnText}}</button>
</template> </template>
......
<script setup> <script setup>
function register(){ import axios from "axios";
function registerUser(credentials) {
axios.post('http://localhost:8080/api/register', credentials)
.then(response => {
console.log("Sikeres regisztráció!")
})
.catch(error => {
console.error('Sikertelen regisztráció!', error);
});
} }
function register(){
registerUser({fullName: 'Gipsz Jakab', username: 'gipja', password: 'keksz'})
}
</script> </script>
<template> <template>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment