Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ public ResponseEntity<AppUser> updateUserDetails(@RequestBody AppUser appUser, @
return new ResponseEntity<>(appUser, HttpStatus.OK);
}

@PutMapping("/{id}/username")
@PutMapping("/{id}/update/{username}")
public ResponseEntity<String> updateUsername(@PathVariable Long id,
@RequestBody AppUser appUser) {
UserEnum result = userService.updateUsername(id, appUser);
@PathVariable String username,
@RequestHeader("Authorization") String token) {
UserEnum result = userService.updateUsername(id, username, token.substring(7));
return resultResponse(result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,36 @@ public UserEnum updateUsername(Long id, AppUser user){
return UserEnum.SUCCESS;
}

public UserEnum updateUsername(Long id, String username, String token){

boolean isValid = isValidToken(token, id);
if(!isValid){
return UserEnum.UNAUTHORIZED;
}

if(username.isEmpty()
|| !RegisterRequirementsUtils.isValidUsername(username)
|| username.length() < 3
|| username.length() > 20
){
System.out.println(username.length());
System.out.println(RegisterRequirementsUtils.isValidUsername(username));
return UserEnum.BAD_USERNAME;
}
;
Optional<AppUser> userOptional = userRepo.findById(id);
if(userOptional.isEmpty()){
return UserEnum.UNKNOWN;
}

AppUser newUser = userOptional.get();

newUser.setUsername(username);
userRepo.save(newUser);

return UserEnum.SUCCESS;
}

/**
* Used to change the user display name.
// * @param id Take in a user id to find the user.
Expand Down
6 changes: 6 additions & 0 deletions project2-front/src/page/SettingPage.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@
.SettingPageChangeNameButton {
margin-top: 2% !important;
width: 27% !important;
}

p {
color: lightgray;
margin-top: -5px;
margin-bottom: -4px;
}
34 changes: 21 additions & 13 deletions project2-front/src/page/SettingPage.jsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
import { Box, Button, InputAdornment, OutlinedInput } from "@mui/material";
import { Link, useNavigate } from "react-router-dom"
import useNav from "../component/Navbar/NavContext/UseNav.jsx";
import EmailIcon from "@mui/icons-material/Email";
import LogOut from "../component/LogOut/LogOut.jsx";

import Cookies from "js-cookie";
import { useState } from "react";
import { projectApi } from "../util/axios.js";
import "./SettingPage.css"

const SettingsPage = () => {
const [displayName, setDisplayName] = useState();
const [username, setUsername] = useState();
const {currentNav} = useNav();
let navigate = useNavigate();

const changeName = async (username) => {
if (displayName === undefined) {
const changeName = async (id) => {
const token = Cookies.get("jwt");
if (username === undefined) {
return [];
}

try {
const response = await projectApi.put(
`/user/${username}/display_name`,
{
displayName: displayName,
},
`/user/${id}/update/${username}`,

{
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
}
Expand All @@ -34,26 +38,30 @@ const SettingsPage = () => {
};

const handleDisplayNameChange = (e) => {
setDisplayName(e.target.value);
setUsername(e.target.value);
};

const handleDisplayChange = async () => {
await changeName(displayName);
await changeName(Cookies.get("user_id"));
Cookies.remove('jwt')
Cookies.remove('username')
navigate('/')
window.location.reload(true)
};

return (
<Box className="SettingPageContainer">
<OutlinedInput className="SettingPageInputBar text" required type="text"
value={displayName}
value={username}
onChange={(e) => handleDisplayNameChange(e)}
placeholder="Enter new display name"
placeholder="Enter new username"
startAdornment={
<InputAdornment position="start">
<EmailIcon />
</InputAdornment>
}
/>

<p>Note: Changing your username will log you out.</p>
<Button className="SettingPageChangeNameButton" variant="contained" onClick={handleDisplayChange}>
Change Name
</Button>
Expand Down
Loading