Skip to content

Commit

Permalink
BACKLOG-23532: refactored login form (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
GauBen authored Jan 15, 2025
1 parent 0c85dd1 commit 4e3e4b5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/client/LoginComponent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const LoginComponent = ({isLoggedIn, userHydrated, urls, mode, nodePath, isShowR
}
};

const logout = () => {
fetch(urls.logoutUrl);
const logout = async () => {
await fetch(urls.logoutUrl);
setLoggedIn(false);
};

Expand Down
62 changes: 31 additions & 31 deletions src/client/LoginForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,42 @@ const LoginForm = ({loginUrl, close, setUser, setLoggedIn, siteKey, isShowRememb
const [password, setPassword] = useState('');
const [rememberMe, setRememberMe] = useState(false);

const login = () => {
const body = [
'username=' + username,
'password=' + password
];
const login = async () => {
const params = new URLSearchParams();
params.set('restMode', 'true');

if (siteKey) {
params.set('site', siteKey);
}

// Request must be sent as application/x-www-form-urlencoded
const body = new URLSearchParams();
body.set('username', username);
body.set('password', password);

if (rememberMe) {
body.push('useCookie=on');
body.set('useCookie', 'on');
}

fetch(loginUrl + '?restMode=true' + (siteKey ? `&site=${siteKey}` : ''), {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
'allow-redirects': 'false'
},
body: body.join('&')
}).then(response => {
try {
response.body.getReader().read().then(({value}) => {
const decodedValue = new TextDecoder().decode(value);
if (decodedValue === 'OK') {
close();
setUser(username);
setLoggedIn(true);
} else if (decodedValue === 'unauthorized') {
setIncorrectLogin(true);
} else {
throw new Error();
}
});
} catch (e) {
console.error('Login form error : ', e);
setUnknownError(true);
try {
const response = await fetch(loginUrl + '?' + params.toString(), {
method: 'POST',
body
});
const value = await response.text();
if (value === 'OK') {
close();
setUser(username);
setLoggedIn(true);
} else if (value === 'unauthorized') {
setIncorrectLogin(true);
} else {
throw new Error('Unknown error: ' + value);
}
});
} catch (error) {
console.error('Login form error: ', error);
setUnknownError(true);
}
};

return (
Expand Down

0 comments on commit 4e3e4b5

Please sign in to comment.