-
Notifications
You must be signed in to change notification settings - Fork 521
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
66 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |