Handle non-JSON responses in login flow
This commit is contained in:
@@ -9,7 +9,6 @@ const users = JSON.parse(fs.readFileSync('C:/Users/frederik/WebstormProjects/Bru
|
|||||||
app.post('/login', (req, res) => {
|
app.post('/login', (req, res) => {
|
||||||
const {username, password} = req.body;
|
const {username, password} = req.body;
|
||||||
const user = users.find(u => u.username === username && u.password === password);
|
const user = users.find(u => u.username === username && u.password === password);
|
||||||
|
|
||||||
if (user) {
|
if (user) {
|
||||||
res.send('Login erfolgreich');
|
res.send('Login erfolgreich');
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+10
-1
@@ -40,7 +40,16 @@
|
|||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ username, password })
|
body: JSON.stringify({ username, password })
|
||||||
});
|
});
|
||||||
const data = await res.json();
|
|
||||||
|
let data;
|
||||||
|
const ct = res.headers.get('content-type') || '';
|
||||||
|
if (ct.includes('application/json')) {
|
||||||
|
data = await res.json();
|
||||||
|
} else {
|
||||||
|
const text = await res.text();
|
||||||
|
data = { ok: res.ok, error: text || res.statusText };
|
||||||
|
}
|
||||||
|
|
||||||
if (res.ok && data.ok) {
|
if (res.ok && data.ok) {
|
||||||
msg.textContent = 'Erfolgreich authentifiziert!';
|
msg.textContent = 'Erfolgreich authentifiziert!';
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user