mirror of https://github.com/immich-app/immich.git
Use cookies for client requests (#377)
* Use cookie for frontend request * Remove api helper to use SDK * Added error handling to status box * Remove additional places that check for session.user * Refactor sending password * prettier clean up * remove deadcode * Move all authentication requests to the client * refactor upload panel to only fetch assets after the upload panel disappear * Added keydown to remove focus on title change on album viewerpull/378/head
parent
2ebb755f00
commit
83cbf51704
@ -1,6 +1,6 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
plugins: {
|
plugins: {
|
||||||
tailwindcss: {},
|
tailwindcss: {},
|
||||||
autoprefixer: {},
|
autoprefixer: {}
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@ -1,64 +0,0 @@
|
|||||||
type AdminRegistrationResult = Promise<{
|
|
||||||
error?: string;
|
|
||||||
success?: string;
|
|
||||||
user?: {
|
|
||||||
email: string;
|
|
||||||
};
|
|
||||||
}>;
|
|
||||||
|
|
||||||
type LoginResult = Promise<{
|
|
||||||
error?: string;
|
|
||||||
success?: string;
|
|
||||||
user?: {
|
|
||||||
accessToken: string;
|
|
||||||
firstName: string;
|
|
||||||
lastName: string;
|
|
||||||
isAdmin: boolean;
|
|
||||||
id: string;
|
|
||||||
email: string;
|
|
||||||
shouldChangePassword: boolean;
|
|
||||||
};
|
|
||||||
}>;
|
|
||||||
|
|
||||||
type UpdateResult = Promise<{
|
|
||||||
error?: string;
|
|
||||||
success?: string;
|
|
||||||
user?: {
|
|
||||||
accessToken: string;
|
|
||||||
firstName: string;
|
|
||||||
lastName: string;
|
|
||||||
isAdmin: boolean;
|
|
||||||
id: string;
|
|
||||||
email: string;
|
|
||||||
};
|
|
||||||
}>;
|
|
||||||
|
|
||||||
export async function sendRegistrationForm(form: HTMLFormElement): AdminRegistrationResult {
|
|
||||||
const response = await fetch(form.action, {
|
|
||||||
method: form.method,
|
|
||||||
body: new FormData(form),
|
|
||||||
headers: { accept: 'application/json' },
|
|
||||||
});
|
|
||||||
|
|
||||||
return await response.json();
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function sendLoginForm(form: HTMLFormElement): LoginResult {
|
|
||||||
const response = await fetch(form.action, {
|
|
||||||
method: form.method,
|
|
||||||
body: new FormData(form),
|
|
||||||
headers: { accept: 'application/json' },
|
|
||||||
});
|
|
||||||
|
|
||||||
return await response.json();
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function sendUpdateForm(form: HTMLFormElement): UpdateResult {
|
|
||||||
const response = await fetch(form.action, {
|
|
||||||
method: form.method,
|
|
||||||
body: new FormData(form),
|
|
||||||
headers: { accept: 'application/json' },
|
|
||||||
});
|
|
||||||
|
|
||||||
return await response.json();
|
|
||||||
}
|
|
||||||
@ -1,59 +0,0 @@
|
|||||||
import { serverEndpoint } from '../constants';
|
|
||||||
|
|
||||||
type ISend = {
|
|
||||||
method: string;
|
|
||||||
path: string;
|
|
||||||
data?: any;
|
|
||||||
token: string;
|
|
||||||
customHeaders?: Record<string, string>;
|
|
||||||
};
|
|
||||||
|
|
||||||
type IOption = {
|
|
||||||
method: string;
|
|
||||||
headers: Record<string, string>;
|
|
||||||
body: any;
|
|
||||||
};
|
|
||||||
|
|
||||||
async function send({ method, path, data, token, customHeaders }: ISend) {
|
|
||||||
const opts: IOption = { method, headers: {} } as IOption;
|
|
||||||
|
|
||||||
if (data) {
|
|
||||||
opts.headers['Content-Type'] = 'application/json';
|
|
||||||
opts.body = JSON.stringify(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (customHeaders) {
|
|
||||||
console.log(customHeaders);
|
|
||||||
// opts.headers[customHeader.$1]
|
|
||||||
}
|
|
||||||
|
|
||||||
if (token) {
|
|
||||||
opts.headers['Authorization'] = `Bearer ${token}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
return fetch(`${serverEndpoint}/${path}`, opts)
|
|
||||||
.then((r) => r.text())
|
|
||||||
.then((json) => {
|
|
||||||
try {
|
|
||||||
return JSON.parse(json);
|
|
||||||
} catch (err) {
|
|
||||||
return json;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getRequest(path: string, token: string, customHeaders?: Record<string, string>) {
|
|
||||||
return send({ method: 'GET', path, token, customHeaders });
|
|
||||||
}
|
|
||||||
|
|
||||||
export function delRequest(path: string, token: string, customHeaders?: Record<string, string>) {
|
|
||||||
return send({ method: 'DELETE', path, token, customHeaders });
|
|
||||||
}
|
|
||||||
|
|
||||||
export function postRequest(path: string, data: any, token: string, customHeaders?: Record<string, string>) {
|
|
||||||
return send({ method: 'POST', path, data, token, customHeaders });
|
|
||||||
}
|
|
||||||
|
|
||||||
export function putRequest(path: string, data: any, token: string, customHeaders?: Record<string, string>) {
|
|
||||||
return send({ method: 'PUT', path, data, token, customHeaders });
|
|
||||||
}
|
|
||||||
@ -1,34 +0,0 @@
|
|||||||
import type { RequestHandler } from '@sveltejs/kit';
|
|
||||||
import { api } from '@api';
|
|
||||||
|
|
||||||
export const POST: RequestHandler = async ({ request }) => {
|
|
||||||
const form = await request.formData();
|
|
||||||
|
|
||||||
const email = form.get('email');
|
|
||||||
const password = form.get('password');
|
|
||||||
const firstName = form.get('firstName');
|
|
||||||
const lastName = form.get('lastName');
|
|
||||||
|
|
||||||
const { status } = await api.userApi.createUser({
|
|
||||||
email: String(email),
|
|
||||||
password: String(password),
|
|
||||||
firstName: String(firstName),
|
|
||||||
lastName: String(lastName)
|
|
||||||
});
|
|
||||||
|
|
||||||
if (status === 201) {
|
|
||||||
return {
|
|
||||||
status: 201,
|
|
||||||
body: {
|
|
||||||
success: 'Succesfully create user account'
|
|
||||||
}
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
return {
|
|
||||||
status: 400,
|
|
||||||
body: {
|
|
||||||
error: 'Error create user account'
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@ -1,38 +0,0 @@
|
|||||||
import type { RequestHandler } from '@sveltejs/kit';
|
|
||||||
import { api } from '@api';
|
|
||||||
|
|
||||||
export const POST: RequestHandler = async ({ request, locals }) => {
|
|
||||||
if (!locals.user) {
|
|
||||||
return {
|
|
||||||
status: 401,
|
|
||||||
body: {
|
|
||||||
error: 'Unauthorized'
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const form = await request.formData();
|
|
||||||
const password = form.get('password');
|
|
||||||
|
|
||||||
const { status } = await api.userApi.updateUser({
|
|
||||||
id: locals.user.id,
|
|
||||||
password: String(password),
|
|
||||||
shouldChangePassword: false
|
|
||||||
});
|
|
||||||
|
|
||||||
if (status === 200) {
|
|
||||||
return {
|
|
||||||
status: 200,
|
|
||||||
body: {
|
|
||||||
success: 'Succesfully change password'
|
|
||||||
}
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
return {
|
|
||||||
status: 400,
|
|
||||||
body: {
|
|
||||||
error: 'Error change password'
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@ -1,59 +0,0 @@
|
|||||||
import type { RequestHandler } from '@sveltejs/kit';
|
|
||||||
import * as cookie from 'cookie';
|
|
||||||
import { api } from '@api';
|
|
||||||
|
|
||||||
export const POST: RequestHandler = async ({ request }) => {
|
|
||||||
const form = await request.formData();
|
|
||||||
|
|
||||||
const email = form.get('email');
|
|
||||||
const password = form.get('password');
|
|
||||||
|
|
||||||
try {
|
|
||||||
const { data: authUser } = await api.authenticationApi.login({
|
|
||||||
email: String(email),
|
|
||||||
password: String(password)
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
|
||||||
status: 200,
|
|
||||||
body: {
|
|
||||||
user: {
|
|
||||||
id: authUser.userId,
|
|
||||||
accessToken: authUser.accessToken,
|
|
||||||
firstName: authUser.firstName,
|
|
||||||
lastName: authUser.lastName,
|
|
||||||
isAdmin: authUser.isAdmin,
|
|
||||||
email: authUser.userEmail,
|
|
||||||
shouldChangePassword: authUser.shouldChangePassword
|
|
||||||
},
|
|
||||||
success: 'success'
|
|
||||||
},
|
|
||||||
headers: {
|
|
||||||
'Set-Cookie': cookie.serialize(
|
|
||||||
'session',
|
|
||||||
JSON.stringify({
|
|
||||||
id: authUser.userId,
|
|
||||||
accessToken: authUser.accessToken,
|
|
||||||
firstName: authUser.firstName,
|
|
||||||
lastName: authUser.lastName,
|
|
||||||
isAdmin: authUser.isAdmin,
|
|
||||||
email: authUser.userEmail
|
|
||||||
}),
|
|
||||||
{
|
|
||||||
path: '/',
|
|
||||||
httpOnly: true,
|
|
||||||
sameSite: 'strict',
|
|
||||||
maxAge: 60 * 60 * 24 * 30
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
} catch (error) {
|
|
||||||
return {
|
|
||||||
status: 400,
|
|
||||||
body: {
|
|
||||||
error: 'Incorrect email or password'
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@ -1,34 +0,0 @@
|
|||||||
import type { RequestHandler } from '@sveltejs/kit';
|
|
||||||
import { api } from '@api';
|
|
||||||
|
|
||||||
export const POST: RequestHandler = async ({ request }) => {
|
|
||||||
const form = await request.formData();
|
|
||||||
|
|
||||||
const email = form.get('email');
|
|
||||||
const password = form.get('password');
|
|
||||||
const firstName = form.get('firstName');
|
|
||||||
const lastName = form.get('lastName');
|
|
||||||
|
|
||||||
const { status } = await api.authenticationApi.adminSignUp({
|
|
||||||
email: String(email),
|
|
||||||
password: String(password),
|
|
||||||
firstName: String(firstName),
|
|
||||||
lastName: String(lastName)
|
|
||||||
});
|
|
||||||
|
|
||||||
if (status === 201) {
|
|
||||||
return {
|
|
||||||
status: 201,
|
|
||||||
body: {
|
|
||||||
success: 'Succesfully create admin account'
|
|
||||||
}
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
return {
|
|
||||||
status: 400,
|
|
||||||
body: {
|
|
||||||
error: 'Error create admin account'
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@ -1,19 +1,21 @@
|
|||||||
<script context="module" lang="ts">
|
<script context="module" lang="ts">
|
||||||
export const prerender = false;
|
export const prerender = false;
|
||||||
|
|
||||||
|
import { api } from '@api';
|
||||||
import type { Load } from '@sveltejs/kit';
|
import type { Load } from '@sveltejs/kit';
|
||||||
|
|
||||||
export const load: Load = async ({ session }) => {
|
export const load: Load = async () => {
|
||||||
if (!session.user) {
|
try {
|
||||||
|
await api.userApi.getMyUserInfo();
|
||||||
return {
|
return {
|
||||||
status: 302,
|
status: 302,
|
||||||
redirect: '/auth/login',
|
redirect: '/photos'
|
||||||
};
|
};
|
||||||
}
|
} catch (e) {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
status: 302,
|
status: 302,
|
||||||
redirect: '/photos',
|
redirect: '/auth/login'
|
||||||
};
|
};
|
||||||
|
}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Loading…
Reference in New Issue