Skip to content

Commit

Permalink
register error
Browse files Browse the repository at this point in the history
  • Loading branch information
KamlaF committed Aug 14, 2023
1 parent c75813e commit 224d930
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 30 deletions.
21 changes: 13 additions & 8 deletions src/js/api/auth/register.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { apiPath } from "../constants.js";
import { headers } from "../headers.js";
import { apiPath } from '../constants.js';
import { headers } from '../headers.js';

export async function register(name, email, password, avatar) {
console.log('Sending registration data:', { name, email, password, avatar });

const response = await fetch(`${apiPath}/social/auth/register`, {
method: "post",
method: 'post',
body: JSON.stringify({ name, email, password, avatar }),
headers: headers("application/json")
})
headers: headers('application/json'),
});

console.log('Received response:', response);

if (response.ok) {
return await response.json()
return await response.json();
}

throw new Error(response.statusText)
}
const responseBody = await response.json();
throw new Error(responseBody.message || response.statusText);
}
37 changes: 18 additions & 19 deletions src/js/listeners/auth/register.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import * as auth from "../../api/auth/index.js";
import * as auth from '../../api/auth/index.js';

export async function registerListener(event) {
event.preventDefault()
const form = event.target
const data = new FormData(form)
const email = data.get("email")
const name = data.get("name")
const password = data.get("password")
const avatar = data.get("avatar")
event.preventDefault();
const form = event.target;
const data = new FormData(form);
const email = data.get('email');
const name = data.get('name');
const password = data.get('password');
const avatar = data.get('avatar');

try {
await auth.register(name, email, password, avatar)
} catch {
return alert("There was a problem creating your account")
const response = await auth.register(name, email, password, avatar);
if (!response.ok) {
const responseBody = await response.json();
console.error('Registration Error:', responseBody);
return alert('There was a problem creating your account');
}
} catch (error) {
console.error('Error:', error);
return alert('There was a problem creating your account');
}

try {
await auth.login(email, password)
location.reload()
} catch {
return alert("There was a problem logging into your new account")
}
}
}
6 changes: 5 additions & 1 deletion src/js/storage/remove.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export const remove = (key) => localStorage.removeItem(key)
export const remove = (key) => localStorage.removeItem(key);

export const logout = () => {
remove('authToken');
};
32 changes: 30 additions & 2 deletions src/js/storage/save.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
export const save = (key, value) => {
localStorage.setItem(key, JSON.stringify(value))
}
localStorage.setItem(key, JSON.stringify(value));
};

export const login = async (username, password) => {
try {
const response = await fetch('/api/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
username: username,
password: password,
}),
});

if (response.ok) {
const data = await response.json();
if (data.token) {
save('authToken', data.token);
} else {
console.error('Token not found in response');
}
} else {
console.error('Login failed', response.statusText);
}
} catch (error) {
console.error('An error occurred:', error);
}
};

0 comments on commit 224d930

Please sign in to comment.