{"id":335204,"date":"2022-06-30T21:00:19","date_gmt":"2022-06-30T21:00:19","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=335204"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=335204","title":{"rendered":"<span>\u0422\u0443\u0442\u043e\u0440\u0438\u0430\u043b: SvelteKit JWT \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435, \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u0432\u043d\u0435\u0434\u0440\u0438\u0442\u044c \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e \u0432 \u0432\u0430\u0448 SvelteKit \u043f\u0440\u043e\u0435\u043a\u0442. \u042d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 JWT \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c <a href=\"https:\/\/habr.com\/ru\/company\/Voximplant\/blog\/323160\/\" rel=\"noopener noreferrer nofollow\">refresh \u0442\u043e\u043a\u0435\u043d\u043e\u0432<\/a> \u0434\u043b\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438. \u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <a href=\"https:\/\/habr.com\/ru\/company\/timeweb\/blog\/648761\/\" rel=\"noopener noreferrer nofollow\">Supabase<\/a> \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 (<a href=\"https:\/\/habr.com\/ru\/post\/340460\/\" rel=\"noopener noreferrer nofollow\">PostgreSQL<\/a>), \u043d\u043e \u043e\u0441\u043d\u043e\u0432\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0442\u0435\u043c\u0438 \u0436\u0435.<\/p>\n<p><a href=\"https:\/\/github.com\/pilcrowOnPaper\/sveltekit-auth-demo\" rel=\"noopener noreferrer nofollow\"><u>Github repository<\/u><\/a><\/p>\n<h2>\u041a\u0430\u043a \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c?<\/h2>\n<p>\u041a\u043e\u0433\u0434\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u043c\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435 \u0438 \u043f\u0430\u0440\u043e\u043b\u044c \u0432 \u043d\u0430\u0448\u0435\u0439 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445.  \u0422\u0430\u043a\u0436\u0435 \u043c\u044b \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c refresh \u0442\u043e\u043a\u0435\u043d \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u043c \u0435\u0433\u043e \u043a\u0430\u043a \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e, \u0442\u0430\u043a \u0438 \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. \u041c\u044b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c JWT \u0442\u043e\u043a\u0435\u043d \u0441 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439 \u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u043c \u0435\u0433\u043e \u0432 \u0432\u0438\u0434\u0435 <code>cookie<\/code>. \u0421\u0440\u043e\u043a \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u044d\u0442\u043e\u0433\u043e JWT \u0442\u043e\u043a\u0435\u043d\u0430 \u0438\u0441\u0442\u0435\u043a\u0430\u0435\u0442 \u0447\u0435\u0440\u0435\u0437 15 \u043c\u0438\u043d\u0443\u0442. \u041a\u043e\u0433\u0434\u0430 \u0441\u0440\u043e\u043a \u0435\u0433\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0438\u0441\u0442\u0435\u0447\u0435\u0442, \u043c\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043b\u0438 refresh \u0442\u043e\u043a\u0435\u043d, \u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u043c \u0435\u0433\u043e \u0441 \u0442\u0435\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d \u0432 \u043d\u0430\u0448\u0435\u0439 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. \u0415\u0441\u043b\u0438 \u043e\u043d \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 JWT \u0442\u043e\u043a\u0435\u043d. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u044d\u0442\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043e\u0442\u043e\u0437\u0432\u0430\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043a \u0432\u0430\u0448\u0435\u043c\u0443 \u0432\u0435\u0431-\u0441\u0430\u0439\u0442\u0443, \u0438\u0437\u043c\u0435\u043d\u0438\u0432 refresh \u0442\u043e\u043a\u0435\u043d, \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0439 \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 (\u0445\u043e\u0442\u044f \u044d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043d\u044f\u0442\u044c \u0434\u043e 15 \u043c\u0438\u043d\u0443\u0442).<\/p>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u043f\u043e\u0447\u0435\u043c\u0443 Supabase, \u0430 \u043d\u0435 Firebase? \u041b\u0438\u0447\u043d\u043e \u044f \u0441\u0447\u0438\u0442\u0430\u044e, \u0447\u0442\u043e \u043d\u0435\u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u0435 \u0447\u0442\u0435\u043d\u0438\u0435 \/ \u0437\u0430\u043f\u0438\u0441\u044c \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0432\u0430\u0436\u043d\u0435\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430 \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u043e\u0439. \u041d\u043e \u043b\u044e\u0431\u0430\u044f \u0431\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043e\u043b\u0436\u043d\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c.<\/p>\n<h2>I. \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430<\/h2>\n<p>\u042d\u0442\u043e\u0442 \u043f\u0440\u043e\u0435\u043a\u0442 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u0442\u044c \u0438\u0437 3-\u0445 \u0441\u0442\u0440\u0430\u043d\u0438\u0446<\/p>\n<ul>\n<li>\n<p><code>index.svelte<\/code>: \u0421\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430<\/p>\n<\/li>\n<li>\n<p><code>signin.svelte<\/code>: \u0421\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u0432\u0445\u043e\u0434\u0430<\/p>\n<\/li>\n<li>\n<p><code>signup.svelte<\/code>: \u0421\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438<\/p>\n<\/li>\n<\/ul>\n<p>\u041d\u0443 \u0438 \u043f\u0430\u043a\u0435\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c<\/p>\n<ul>\n<li>\n<p><code>supabase<\/code><\/p>\n<\/li>\n<li>\n<p><code>bcrypt<\/code>: \u0414\u043b\u044f \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0430\u0440\u043e\u043b\u0435\u0439<\/p>\n<\/li>\n<li>\n<p><code>crypto<\/code>: \u0414\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 <code>user id<\/code> (UUID)<\/p>\n<\/li>\n<li>\n<p><code>jsonwebtoken<\/code>: \u0414\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f JWT<\/p>\n<\/li>\n<li>\n<p><code>cookie<\/code>: \u0414\u043b\u044f \u043f\u0430\u0440\u0441\u0438\u043d\u0433\u0430 <code>cookie<\/code> \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430<\/p>\n<\/li>\n<\/ul>\n<h2>II. Supabase<\/h2>\n<p>\u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043d\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442. \u0422\u0435\u043f\u0435\u0440\u044c \u0441\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043d\u043e\u0432\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 <code>users<\/code> (\u0432\u0441\u0451 <code>non-null<\/code>).<\/p>\n<ul>\n<li>\n<p><code>id<\/code>\u00a0: int8, unique, isIdentity<\/p>\n<\/li>\n<li>\n<p><code>email<\/code>\u00a0: varchar, unique<\/p>\n<\/li>\n<li>\n<p><code>password<\/code>\u00a0: text<\/p>\n<\/li>\n<li>\n<p><code>username<\/code>\u00a0: varchar, unique<\/p>\n<\/li>\n<li>\n<p><code>user_id<\/code>\u00a0: uuid, unique<\/p>\n<\/li>\n<li>\n<p><code>refresh_token<\/code>\u00a0: text<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0435\u0440\u0435\u0439\u0434\u0438\u0442\u0435 \u0432 settings > api. \u0421\u043a\u043e\u043f\u0438\u0440\u0443\u0439\u0442\u0435 <code>service_role<\/code> \u0438 <code>URL<\/code>. \u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 <code>supabase-admin.ts<\/code>:<\/p>\n<pre><code class=\"typescript\">import { createClient } from '@supabase\/supabase-js';  export const admin = createClient(     'URL',     'service_role' );<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 Supabase, \u041d\u0415 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u044d\u0442\u043e\u0433\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0430 (<code>admin<\/code>). \u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0441\u0432\u043e\u0439 <code>anon<\/code> \u043a\u043b\u044e\u0447.<\/p>\n<h2>III. \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0443\u0447\u0451\u0442\u043d\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438 (\u0430\u043a\u043a\u0430\u0443\u043d\u0442\u0430)<\/h2>\n<p>\u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043d\u043e\u0432\u044b\u0439 \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442 (<code>\/api\/create-user.ts<\/code>). \u041e\u043d \u0431\u0443\u0434\u0435\u0442 \u0434\u043b\u044f <code>POST<\/code> \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u0438 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0435\u0433\u043e <code>body<\/code>(\u0442\u0435\u043b\u0430) \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u044e\u0442\u0441\u044f <code>email<\/code>, <code>password<\/code> \u0438 <code>username<\/code>.<\/p>\n<pre><code class=\"typescript\">export const post: RequestHandler = async (event) => {     const body = (await event.request.json()) as Body;     if (!body.email || !body.password || !body.username) return returnError(400, 'Invalid request');     if (!validateEmail(body.email) || body.username.length &lt; 4 || body.password.length &lt; 6)         return returnError(400, 'Bad request'); }<\/code><\/pre>\n<p>\u041a\u0441\u0442\u0430\u0442\u0438, <code>returnError()<\/code> \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043a\u043e\u0434 \u0447\u0438\u0449\u0435. \u0418 <code>validateEmail()<\/code> \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442, \u0435\u0441\u0442\u044c \u043b\u0438 \u0432 \u0441\u0442\u0440\u043e\u043a\u0435 <code>@<\/code>, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 (\u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u043d\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e) \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u043d\u0430 100% \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 email \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u043d\u043e\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435.<\/p>\n<pre><code class=\"typescript\">export const returnError = (status: number, message: string): RequestHandlerOutput => {     return {         status,         body: {             message         }     }; };<\/code><\/pre>\n<p>\u0412 \u043b\u044e\u0431\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0443\u0431\u0435\u0434\u0438\u043c\u0441\u044f, \u0447\u0442\u043e <code>email<\/code> \u0438\u043b\u0438 <code>username<\/code> \u0435\u0449\u0435 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f.<\/p>\n<pre><code class=\"typescript\">const check_user = await admin     .from('users')     .select()     .or(`email.eq.${body.email},username.eq.${body.username}`)     .maybeSingle() if (check_user.data) return returnError(405, 'User already exists');<\/code><\/pre>\n<p>\u0417\u0430\u0442\u0435\u043c \u0445\u0435\u0448\u0438\u0440\u0443\u0435\u043c \u043f\u0430\u0440\u043e\u043b\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f (<code>password<\/code>) \u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u043e\u0432\u044b\u0439 <code>user_id<\/code> (UUID) \u0438 refresh \u0442\u043e\u043a\u0435\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d \u0432 \u043d\u0430\u0448\u0435\u0439 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<pre><code class=\"typescript\">const salt = await bcrypt.genSalt(10); const hash = await bcrypt.hash(body.password, salt); const user_id = randomUUID(); \/\/ import { randomUUID } from 'crypto'; const refresh_token = randomUUID(); const create_user = await admin.from('users').insert([     {         email: body.email,         username: body.username,         password: hash,         user_id,         refresh_token     } ]); if (create_user.error) return returnError(500, create_user.statusText);<\/code><\/pre>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0439\u0442\u0435 \u043d\u043e\u0432\u044b\u0439 JWT \u0442\u043e\u043a\u0435\u043d. \u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u0431\u0435\u0440\u0438\u0442\u0435 \u0447\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0435 \u0434\u043b\u044f \u043a\u043b\u044e\u0447\u0430. \u0423\u0431\u0435\u0434\u0438\u0442\u0435\u0441\u044c, \u0447\u0442\u043e \u0432\u044b \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u0438 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0439 \u0440\u0435\u0436\u0438\u043c (<code>Secure<\/code>) \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u0432\u044b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0435\u0441\u044c \u0440\u0435\u0436\u0438\u043c\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 (localhost &#8212; \u044d\u0442\u043e http, \u0430 \u043d\u0435 https).<\/p>\n<pre><code class=\"typescript\">const user = {     username: body.username,     user_id,     email: body.email }; const secure = dev ? '' : ' Secure;'; \/\/ import * as jwt from 'jsonwebtoken'; \/\/ expires in 15 minutes const token = jwt.sign(user, key, { expiresIn: `${15 * 60 * 1000}` }); return {     status: 200,     headers: {         \/\/ import { dev } from '$app\/env';         \/\/ const secure = dev ? '' : ' Secure;';         'set-cookie': [             \/\/ expires in 90 days             `refresh_token=${refresh_token}; Max-Age=${30 * 24 * 60 * 60}; Path=\/; ${secure} HttpOnly`,             `token=${token}; Max-Age=${15 * 60}; Path=\/;${secure} HttpOnly`         ]     } };<\/code><\/pre>\n<p>\u041d\u0430 \u043d\u0430\u0448\u0435\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0437\u0432\u0430\u0442\u044c <code>POST<\/code>-\u0437\u0430\u043f\u0440\u043e\u0441 \u0438 \u043f\u0435\u0440\u0435\u043d\u0430\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0435\u0441\u043b\u0438 \u043e\u043d \u043f\u0440\u043e\u0439\u0434\u0435\u0442 \u0443\u0441\u043f\u0435\u0448\u043d\u043e. \u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 <code>window.location.href<\/code> \u0432\u043c\u0435\u0441\u0442\u043e <code>goto()<\/code>, \u0438\u043d\u0430\u0447\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 (\u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 <code>cookie<\/code>) \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u043e.<\/p>\n<pre><code class=\"typescript\">const signUp = async () => {     const response = await fetch('\/api\/create-user', {         method: 'POST',         credentials: 'same-origin',         body: JSON.stringify({             email,             username,             password         })     });     if (response.ok) {         window.location.href = '\/';     } };<\/code><\/pre>\n<h3>IV. \u0412\u0445\u043e\u0434<\/h3>\n<p>\u041c\u044b \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0432\u0445\u043e\u0434 \u0432 <code>\/api\/signin.ts<\/code>. \u041d\u0430 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u043c\u044b \u0440\u0430\u0437\u0440\u0435\u0448\u0438\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043b\u0438\u0431\u043e \u0441\u0432\u043e\u0435 \u0438\u043c\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f (<code>username<\/code>), \u043b\u0438\u0431\u043e \u0430\u0434\u0440\u0435\u0441 \u044d\u043b\u0435\u043a\u0442\u0440\u043e\u043d\u043d\u043e\u0439 \u043f\u043e\u0447\u0442\u044b (<code>email<\/code>). \u0427\u0442\u043e\u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u044d\u0442\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u0438\u043c\u0435\u043d\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u043b\u0438 \u0430\u0434\u0440\u0435\u0441\u043e\u043c \u044d\u043b\u0435\u043a\u0442\u0440\u043e\u043d\u043d\u043e\u0439 \u043f\u043e\u0447\u0442\u044b, \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043b\u0438 \u0442\u0430\u043a\u043e\u0435 \u0436\u0435 \u0438\u043c\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u043b\u0438 \u0430\u0434\u0440\u0435\u0441 \u044d\u043b\u0435\u043a\u0442\u0440\u043e\u043d\u043d\u043e\u0439 \u043f\u043e\u0447\u0442\u044b<\/p>\n<pre><code class=\"typescript\">export const post: RequestHandler = async (event) => {     const body = (await event.request.json()) as Body;     if (!body.email_username || !body.password) return returnError(400, 'Invalid request');     const valid_email = body.email_username.includes('@') &amp;&amp; validateEmail(body.email_username);     const valid_username = !body.email_username.includes('@') &amp;&amp; body.email_username.length > 3;     if ((!valid_email &amp;&amp; !valid_username) || body.password.length &lt; 6)         return returnError(400, 'Bad request');     const getUser = await admin         .from('users')         .select()         .or(`username.eq.${body.email_username},email.eq.${body.email_username}`)         .maybeSingle()     if (!getUser.data) return returnError(405, 'User does not exist'); }<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043c\u044b \u0441\u0440\u0430\u0432\u043d\u0438\u043c \u0432\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0439 \u043f\u0430\u0440\u043e\u043b\u044c.<\/p>\n<pre><code class=\"typescript\">const user_data = getUser.data as Users_Table; const authenticated = await bcrypt.compare(body.password, user_data.password); if (!authenticated) return returnError(401, 'Incorrect password');<\/code><\/pre>\n<p>\u0418, \u043d\u0430\u043a\u043e\u043d\u0435\u0446, \u0441\u0434\u0435\u043b\u0430\u0439\u0442\u0435 \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435, \u0447\u0442\u043e \u0438 \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043d\u043e\u0432\u043e\u0439 \u0443\u0447\u0435\u0442\u043d\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438.<\/p>\n<pre><code class=\"typescript\">const refresh_token = user_data.refresh_token; const user = {     username: user_data.username,     user_id: user_data.user_id,     email: user_data.email }; const token = jwt.sign(user, key, { expiresIn: `${expiresIn * 60 * 1000}` }); return {     status: 200,     headers: {         'set-cookie': [             `refresh_token=${refresh_token}; Max-Age=${refresh_token_expiresIn * 24 * 60 * 60}; Path=\/; ${secure} HttpOnly`,             `token=${token}; Max-Age=${15 * 60}; Path=\/;${secure} HttpOnly`         ]     } };<\/code><\/pre>\n<h3>V. \u0410\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439<\/h3>\n<p>\u0425\u043e\u0442\u044f \u043c\u044b \u0438 \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0445\u0443\u043a\u0438 \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f JWT \u0442\u043e\u043a\u0435\u043d\u0430 (\u043a\u0430\u043a \u0432 \u044d\u0442\u043e\u0439 <a href=\"https:\/\/dev.to\/pilcrowonpaper\/saving-users-preferences-in-sveltekit-3b37\" rel=\"noopener noreferrer nofollow\">\u0441\u0442\u0430\u0442\u044c\u0435<\/a>, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0430\u0432\u0442\u043e\u0440), \u043c\u044b \u043d\u0435 \u0441\u043c\u043e\u0436\u0435\u043c \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c (\u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c) \u043d\u043e\u0432\u044b\u0439 JWT \u0442\u043e\u043a\u0435\u043d \u0441 \u0438\u0445 \u043f\u043e\u043c\u043e\u0449\u044c\u044e. \u0418\u0442\u0430\u043a, \u043c\u044b \u0432\u044b\u0437\u043e\u0432\u0435\u043c \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0435\u0442 <code>cookie<\/code> \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442 \u0438\u0445, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0432\u0435\u0440\u043d\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0435\u0441\u043b\u0438 \u043e\u043d\u0438 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442. \u042d\u0442\u043e\u0442 \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442 \u0442\u0430\u043a\u0436\u0435 \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0441\u0435\u0430\u043d\u0441\u044b \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f (<code>refreshing sessions<\/code>). \u042d\u0442\u043e\u0442 \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f <code>\/api\/auth.ts<\/code>.<\/p>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c <code>cookie<\/code>, \u0438 \u0435\u0441\u043b\u0438 \u043e\u043d\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u044b &#8212; \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u0415\u0441\u043b\u0438 \u043e\u043d\u0438 \u043d\u0435\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u044b, \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>verify()<\/code> \u0432\u044b\u0434\u0430\u0441\u0442 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0435.<\/p>\n<pre><code class=\"typescript\">export const get: RequestHandler = async (event) => {     const { token, refresh_token } = cookie.parse(event.request.headers.get('cookie') || '');     try {         const user = jwt.verify(token, key) as Record&lt;any, any>;         return {             status: 200,             body: user         };     } catch {         \/\/ invalid or expired token     } }<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0441\u0440\u043e\u043a \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f JWT \u0442\u043e\u043a\u0435\u043d\u0430 \u0438\u0441\u0442\u0435\u043a, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c refresh \u0442\u043e\u043a\u0435\u043d \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0442\u043e\u043a\u0435\u043d\u0430 \u0432 \u043d\u0430\u0448\u0435\u0439 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. \u0415\u0441\u043b\u0438 \u043e\u043d\u0438 \u0440\u0430\u0432\u043d\u044b, \u0442\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 JWT \u0442\u043e\u043a\u0435\u043d.<\/p>\n<pre><code class=\"typescript\">if (!refresh_token) return returnError(401, 'Unauthorized user'); const getUser = await admin.from('users').select().eq(\"refresh_token\", refresh_token).maybeSingle() if (!getUser.data) {     \/\/ remove invalid refresh token     return {         status: 401,         headers: {             'set-cookie': [                 `refresh_token=; Max-Age=0; Path=\/;${secure} HttpOnly`             ]         },     } } const user_data = getUser.data as Users_Table; const new_user = {     username: user_data.username,     user_id: user_data.user_id,     email: user_data.email }; const token = jwt.sign(new_user, key, { expiresIn: `${15 * 60 * 1000}` }); return {     status: 200,     headers: {         'set-cookie': [             `token=${token}; Max-Age=${15 * 60}; Path=\/;${secure} HttpOnly`         ]     }, };<\/code><\/pre>\n<h3>VI. \u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439<\/h3>\n<p>\u0427\u0442\u043e\u0431\u044b \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0443 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0438\u0437 <code>\/api\/auth<\/code> \u0432 <code>load<\/code> \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<\/p>\n<pre><code class=\"typescript\">\/\/ index.sve;te \/\/ inside &lt;script context=\"module\" lang=\"ts\"\/> export const load: Load = async (input) => {     const response = await input.fetch('\/api\/auth');     const user = (await response.json()) as Session;     if (!user.user_id) {         \/\/ user doesn't exist         return {             status: 302,             redirect: '\/signin'         };     }     return {         props: {             user         }     }; };<\/code><\/pre>\n<h3>VII. \u0412\u044b\u0445\u043e\u0434 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u0437 \u0441\u0438\u0441\u0442\u0435\u043c\u044b<\/h3>\n<p>\u0427\u0442\u043e\u0431\u044b \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0441\u0438\u0441\u0442\u0435\u043c\u044b, \u043f\u0440\u043e\u0441\u0442\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u0435 JWT \u0442\u043e\u043a\u0435\u043d \u0438 refresh \u0442\u043e\u043a\u0435\u043d.<\/p>\n<pre><code class=\"typescript\">\/\/ \/api\/signout.ts export const post : RequestHandler = async () => {     return {     status: 200,         headers: {             'set-cookie': [                 `refresh_token=; Max-Age=0; Path=\/; ${secure} HttpOnly`,                 `token=; Max-Age=0; Path=\/;${secure} HttpOnly`             ]         }     }; };<\/code><\/pre>\n<h3>VIII. \u041e\u0442\u0437\u044b\u0432 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f<\/h3>\n<p>\u0427\u0442\u043e\u0431\u044b \u043e\u0442\u043e\u0437\u0432\u0430\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u043f\u0440\u043e\u0441\u0442\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0435 refresh \u0442\u043e\u043a\u0435\u043d \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. \u0418\u043c\u0435\u0439\u0442\u0435 \u0432 \u0432\u0438\u0434\u0443, \u0447\u0442\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0431\u0443\u0434\u0435\u0442 \u043e\u0441\u0442\u0430\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u0434\u043e 15 \u043c\u0438\u043d\u0443\u0442 (<em>\u0441\u0440\u043e\u043a \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f JWT<\/em>).<\/p>\n<pre><code class=\"typescript\">const new_refresh_token = randomUUID(); await admin.from('users').update({ refresh_token: new_refresh_token }).eq(\"refresh_token\", refresh_token);<\/code><\/pre>\n<p>\u042d\u0442\u043e \u043e\u0441\u043d\u043e\u0432\u044b, \u043d\u043e \u0435\u0441\u043b\u0438 \u0432\u044b \u043f\u043e\u043d\u044f\u043b\u0438 \u044d\u0442\u043e, \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0439 \u043f\u0440\u043e\u0444\u0438\u043b\u044f \u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/674456\/\"> https:\/\/habr.com\/ru\/post\/674456\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435, \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u0432\u043d\u0435\u0434\u0440\u0438\u0442\u044c \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e \u0432 \u0432\u0430\u0448 SvelteKit \u043f\u0440\u043e\u0435\u043a\u0442. \u042d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 JWT \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c <a href=\"https:\/\/habr.com\/ru\/company\/Voximplant\/blog\/323160\/\" rel=\"noopener noreferrer nofollow\">refresh \u0442\u043e\u043a\u0435\u043d\u043e\u0432<\/a> \u0434\u043b\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438. \u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <a href=\"https:\/\/habr.com\/ru\/company\/timeweb\/blog\/648761\/\" rel=\"noopener noreferrer nofollow\">Supabase<\/a> \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 (<a href=\"https:\/\/habr.com\/ru\/post\/340460\/\" rel=\"noopener noreferrer nofollow\">PostgreSQL<\/a>), \u043d\u043e \u043e\u0441\u043d\u043e\u0432\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0442\u0435\u043c\u0438 \u0436\u0435.<\/p>\n<p><a href=\"https:\/\/github.com\/pilcrowOnPaper\/sveltekit-auth-demo\" rel=\"noopener noreferrer nofollow\"><u>Github repository<\/u><\/a><\/p>\n<h2>\u041a\u0430\u043a \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c?<\/h2>\n<p>\u041a\u043e\u0433\u0434\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u043c\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435 \u0438 \u043f\u0430\u0440\u043e\u043b\u044c \u0432 \u043d\u0430\u0448\u0435\u0439 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445.  \u0422\u0430\u043a\u0436\u0435 \u043c\u044b \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c refresh \u0442\u043e\u043a\u0435\u043d \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u043c \u0435\u0433\u043e \u043a\u0430\u043a \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e, \u0442\u0430\u043a \u0438 \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. \u041c\u044b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c JWT \u0442\u043e\u043a\u0435\u043d \u0441 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439 \u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u043c \u0435\u0433\u043e \u0432 \u0432\u0438\u0434\u0435 <code>cookie<\/code>. \u0421\u0440\u043e\u043a \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u044d\u0442\u043e\u0433\u043e JWT \u0442\u043e\u043a\u0435\u043d\u0430 \u0438\u0441\u0442\u0435\u043a\u0430\u0435\u0442 \u0447\u0435\u0440\u0435\u0437 15 \u043c\u0438\u043d\u0443\u0442. \u041a\u043e\u0433\u0434\u0430 \u0441\u0440\u043e\u043a \u0435\u0433\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0438\u0441\u0442\u0435\u0447\u0435\u0442, \u043c\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043b\u0438 refresh \u0442\u043e\u043a\u0435\u043d, \u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u043c \u0435\u0433\u043e \u0441 \u0442\u0435\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d \u0432 \u043d\u0430\u0448\u0435\u0439 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. \u0415\u0441\u043b\u0438 \u043e\u043d \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 JWT \u0442\u043e\u043a\u0435\u043d. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u044d\u0442\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043e\u0442\u043e\u0437\u0432\u0430\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043a \u0432\u0430\u0448\u0435\u043c\u0443 \u0432\u0435\u0431-\u0441\u0430\u0439\u0442\u0443, \u0438\u0437\u043c\u0435\u043d\u0438\u0432 refresh \u0442\u043e\u043a\u0435\u043d, \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0439 \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 (\u0445\u043e\u0442\u044f \u044d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043d\u044f\u0442\u044c \u0434\u043e 15 \u043c\u0438\u043d\u0443\u0442).<\/p>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u043f\u043e\u0447\u0435\u043c\u0443 Supabase, \u0430 \u043d\u0435 Firebase? \u041b\u0438\u0447\u043d\u043e \u044f \u0441\u0447\u0438\u0442\u0430\u044e, \u0447\u0442\u043e \u043d\u0435\u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u0435 \u0447\u0442\u0435\u043d\u0438\u0435 \/ \u0437\u0430\u043f\u0438\u0441\u044c \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0432\u0430\u0436\u043d\u0435\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430 \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u043e\u0439. \u041d\u043e \u043b\u044e\u0431\u0430\u044f \u0431\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043e\u043b\u0436\u043d\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c.<\/p>\n<h2>I. \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430<\/h2>\n<p>\u042d\u0442\u043e\u0442 \u043f\u0440\u043e\u0435\u043a\u0442 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u0442\u044c \u0438\u0437 3-\u0445 \u0441\u0442\u0440\u0430\u043d\u0438\u0446<\/p>\n<ul>\n<li>\n<p><code>index.svelte<\/code>: \u0421\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430<\/p>\n<\/li>\n<li>\n<p><code>signin.svelte<\/code>: \u0421\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u0432\u0445\u043e\u0434\u0430<\/p>\n<\/li>\n<li>\n<p><code>signup.svelte<\/code>: \u0421\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438<\/p>\n<\/li>\n<\/ul>\n<p>\u041d\u0443 \u0438 \u043f\u0430\u043a\u0435\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c<\/p>\n<ul>\n<li>\n<p><code>supabase<\/code><\/p>\n<\/li>\n<li>\n<p><code>bcrypt<\/code>: \u0414\u043b\u044f \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0430\u0440\u043e\u043b\u0435\u0439<\/p>\n<\/li>\n<li>\n<p><code>crypto<\/code>: \u0414\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 <code>user id<\/code> (UUID)<\/p>\n<\/li>\n<li>\n<p><code>jsonwebtoken<\/code>: \u0414\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f JWT<\/p>\n<\/li>\n<li>\n<p><code>cookie<\/code>: \u0414\u043b\u044f \u043f\u0430\u0440\u0441\u0438\u043d\u0433\u0430 <code>cookie<\/code> \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430<\/p>\n<\/li>\n<\/ul>\n<h2>II. Supabase<\/h2>\n<p>\u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043d\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442. \u0422\u0435\u043f\u0435\u0440\u044c \u0441\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043d\u043e\u0432\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 <code>users<\/code> (\u0432\u0441\u0451 <code>non-null<\/code>).<\/p>\n<ul>\n<li>\n<p><code>id<\/code>\u00a0: int8, unique, isIdentity<\/p>\n<\/li>\n<li>\n<p><code>email<\/code>\u00a0: varchar, unique<\/p>\n<\/li>\n<li>\n<p><code>password<\/code>\u00a0: text<\/p>\n<\/li>\n<li>\n<p><code>username<\/code>\u00a0: varchar, unique<\/p>\n<\/li>\n<li>\n<p><code>user_id<\/code>\u00a0: uuid, unique<\/p>\n<\/li>\n<li>\n<p><code>refresh_token<\/code>\u00a0: text<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0435\u0440\u0435\u0439\u0434\u0438\u0442\u0435 \u0432 settings > api. \u0421\u043a\u043e\u043f\u0438\u0440\u0443\u0439\u0442\u0435 <code>service_role<\/code> \u0438 <code>URL<\/code>. \u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 <code>supabase-admin.ts<\/code>:<\/p>\n<pre><code class=\"typescript\">import { createClient } from '@supabase\/supabase-js';  export const admin = createClient(     'URL',     'service_role' );<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 Supabase, \u041d\u0415 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u044d\u0442\u043e\u0433\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0430 (<code>admin<\/code>). \u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0441\u0432\u043e\u0439 <code>anon<\/code> \u043a\u043b\u044e\u0447.<\/p>\n<h2>III. \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0443\u0447\u0451\u0442\u043d\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438 (\u0430\u043a\u043a\u0430\u0443\u043d\u0442\u0430)<\/h2>\n<p>\u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043d\u043e\u0432\u044b\u0439 \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442 (<code>\/api\/create-user.ts<\/code>). \u041e\u043d \u0431\u0443\u0434\u0435\u0442 \u0434\u043b\u044f <code>POST<\/code> \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u0438 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0435\u0433\u043e <code>body<\/code>(\u0442\u0435\u043b\u0430) \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u044e\u0442\u0441\u044f <code>email<\/code>, <code>password<\/code> \u0438 <code>username<\/code>.<\/p>\n<pre><code class=\"typescript\">export const post: RequestHandler = async (event) => {     const body = (await event.request.json()) as Body;     if (!body.email || !body.password || !body.username) return returnError(400, 'Invalid request');     if (!validateEmail(body.email) || body.username.length &lt; 4 || body.password.length &lt; 6)         return returnError(400, 'Bad request'); }<\/code><\/pre>\n<p>\u041a\u0441\u0442\u0430\u0442\u0438, <code>returnError()<\/code> \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043a\u043e\u0434 \u0447\u0438\u0449\u0435. \u0418 <code>validateEmail()<\/code> \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442, \u0435\u0441\u0442\u044c \u043b\u0438 \u0432 \u0441\u0442\u0440\u043e\u043a\u0435 <code>@<\/code>, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 (\u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u043d\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e) \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u043d\u0430 100% \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 email \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u043d\u043e\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435.<\/p>\n<pre><code class=\"typescript\">export const returnError = (status: number, message: string): RequestHandlerOutput => {     return {         status,         body: {             message         }     }; };<\/code><\/pre>\n<p>\u0412 \u043b\u044e\u0431\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0443\u0431\u0435\u0434\u0438\u043c\u0441\u044f, \u0447\u0442\u043e <code>email<\/code> \u0438\u043b\u0438 <code>username<\/code> \u0435\u0449\u0435 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f.<\/p>\n<pre><code class=\"typescript\">const check_user = await admin     .from('users')     .select()     .or(`email.eq.${body.email},username.eq.${body.username}`)     .maybeSingle() if (check_user.data) return returnError(405, 'User already exists');<\/code><\/pre>\n<p>\u0417\u0430\u0442\u0435\u043c \u0445\u0435\u0448\u0438\u0440\u0443\u0435\u043c \u043f\u0430\u0440\u043e\u043b\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f (<code>password<\/code>) \u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u043e\u0432\u044b\u0439 <code>user_id<\/code> (UUID) \u0438 refresh \u0442\u043e\u043a\u0435\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d \u0432 \u043d\u0430\u0448\u0435\u0439 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<pre><code class=\"typescript\">const salt = await bcrypt.genSalt(10); const hash = await bcrypt.hash(body.password, salt); const user_id = randomUUID(); \/\/ import { randomUUID } from 'crypto'; const refresh_token = randomUUID(); const create_user = await admin.from('users').insert([     {         email: body.email,         username: body.username,         password: hash,         user_id,         refresh_token     } ]); if (create_user.error) return returnError(500, create_user.statusText);<\/code><\/pre>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0439\u0442\u0435 \u043d\u043e\u0432\u044b\u0439 JWT \u0442\u043e\u043a\u0435\u043d. \u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u0431\u0435\u0440\u0438\u0442\u0435 \u0447\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0435 \u0434\u043b\u044f \u043a\u043b\u044e\u0447\u0430. \u0423\u0431\u0435\u0434\u0438\u0442\u0435\u0441\u044c, \u0447\u0442\u043e \u0432\u044b \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u0438 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0439 \u0440\u0435\u0436\u0438\u043c (<code>Secure<\/code>) \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u0432\u044b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0435\u0441\u044c \u0440\u0435\u0436\u0438\u043c\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 (localhost &#8212; \u044d\u0442\u043e http, \u0430 \u043d\u0435 https).<\/p>\n<pre><code class=\"typescript\">const user = {     username: body.username,     user_id,     email: body.email }; const secure = dev ? '' : ' Secure;'; \/\/ import * as jwt from 'jsonwebtoken'; \/\/ expires in 15 minutes const token = jwt.sign(user, key, { expiresIn: `${15 * 60 * 1000}` }); return {     status: 200,     headers: {         \/\/ import { dev } from '$app\/env';         \/\/ const secure = dev ? '' : ' Secure;';         'set-cookie': [             \/\/ expires in 90 days             `refresh_token=${refresh_token}; Max-Age=${30 * 24 * 60 * 60}; Path=\/; ${secure} HttpOnly`,             `token=${token}; Max-Age=${15 * 60}; Path=\/;${secure} HttpOnly`         ]     } };<\/code><\/pre>\n<p>\u041d\u0430 \u043d\u0430\u0448\u0435\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0437\u0432\u0430\u0442\u044c <code>POST<\/code>-\u0437\u0430\u043f\u0440\u043e\u0441 \u0438 \u043f\u0435\u0440\u0435\u043d\u0430\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0435\u0441\u043b\u0438 \u043e\u043d \u043f\u0440\u043e\u0439\u0434\u0435\u0442 \u0443\u0441\u043f\u0435\u0448\u043d\u043e. \u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 <code>window.location.href<\/code> \u0432\u043c\u0435\u0441\u0442\u043e <code>goto()<\/code>, \u0438\u043d\u0430\u0447\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 (\u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 <code>cookie<\/code>) \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u043e.<\/p>\n<pre><code class=\"typescript\">const signUp = async () => {     const response = await fetch('\/api\/create-user', {         method: 'POST',         credentials: 'same-origin',         body: JSON.stringify({             email,             username,             password         })     });     if (response.ok) {         window.location.href = '\/';     } };<\/code><\/pre>\n<h3>IV. \u0412\u0445\u043e\u0434<\/h3>\n<p>\u041c\u044b \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0432\u0445\u043e\u0434 \u0432 <code>\/api\/signin.ts<\/code>. \u041d\u0430 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u043c\u044b \u0440\u0430\u0437\u0440\u0435\u0448\u0438\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043b\u0438\u0431\u043e \u0441\u0432\u043e\u0435 \u0438\u043c\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f (<code>username<\/code>), \u043b\u0438\u0431\u043e \u0430\u0434\u0440\u0435\u0441 \u044d\u043b\u0435\u043a\u0442\u0440\u043e\u043d\u043d\u043e\u0439 \u043f\u043e\u0447\u0442\u044b (<code>email<\/code>). \u0427\u0442\u043e\u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u044d\u0442\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u0438\u043c\u0435\u043d\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u043b\u0438 \u0430\u0434\u0440\u0435\u0441\u043e\u043c \u044d\u043b\u0435\u043a\u0442\u0440\u043e\u043d\u043d\u043e\u0439 \u043f\u043e\u0447\u0442\u044b, \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043b\u0438 \u0442\u0430\u043a\u043e\u0435 \u0436\u0435 \u0438\u043c\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u043b\u0438 \u0430\u0434\u0440\u0435\u0441 \u044d\u043b\u0435\u043a\u0442\u0440\u043e\u043d\u043d\u043e\u0439 \u043f\u043e\u0447\u0442\u044b<\/p>\n<pre><code class=\"typescript\">export const post: RequestHandler = async (event) => {     const body = (await event.request.json()) as Body;     if (!body.email_username || !body.password) return returnError(400, 'Invalid request');     const valid_email = body.email_username.includes('@') &amp;&amp; validateEmail(body.email_username);     const valid_username = !body.email_username.includes('@') &amp;&amp; body.email_username.length > 3;     if ((!valid_email &amp;&amp; !valid_username) || body.password.length &lt; 6)         return returnError(400, 'Bad request');     const getUser = await admin         .from('users')         .select()         .or(`username.eq.${body.email_username},email.eq.${body.email_username}`)         .maybeSingle()     if (!getUser.data) return returnError(405, 'User does not exist'); }<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043c\u044b \u0441\u0440\u0430\u0432\u043d\u0438\u043c \u0432\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0439 \u043f\u0430\u0440\u043e\u043b\u044c.<\/p>\n<pre><code class=\"typescript\">const user_data = getUser.data as Users_Table; const authenticated = await bcrypt.compare(body.password, user_data.password); if (!authenticated) return returnError(401, 'Incorrect password');<\/code><\/pre>\n<p>\u0418, \u043d\u0430\u043a\u043e\u043d\u0435\u0446, \u0441\u0434\u0435\u043b\u0430\u0439\u0442\u0435 \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435, \u0447\u0442\u043e \u0438 \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043d\u043e\u0432\u043e\u0439 \u0443\u0447\u0435\u0442\u043d\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438.<\/p>\n<pre><code class=\"typescript\">const refresh_token = user_data.refresh_token; const user = {     username: user_data.username,     user_id: user_data.user_id,     email: user_data.email }; const token = jwt.sign(user, key, { expiresIn: `${expiresIn * 60 * 1000}` }); return {     status: 200,     headers: {         'set-cookie': [             `refresh_token=${refresh_token}; Max-Age=${refresh_token_expiresIn * 24 * 60 * 60}; Path=\/; ${secure} HttpOnly`,             `token=${token}; Max-Age=${15 * 60}; Path=\/;${secure} HttpOnly`         ]     } };<\/code><\/pre>\n<h3>V. \u0410\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439<\/h3>\n<p>\u0425\u043e\u0442\u044f \u043c\u044b \u0438 \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0445\u0443\u043a\u0438 \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f JWT \u0442\u043e\u043a\u0435\u043d\u0430 (\u043a\u0430\u043a \u0432 \u044d\u0442\u043e\u0439 <a href=\"https:\/\/dev.to\/pilcrowonpaper\/saving-users-preferences-in-sveltekit-3b37\" rel=\"noopener noreferrer nofollow\">\u0441\u0442\u0430\u0442\u044c\u0435<\/a>, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0430\u0432\u0442\u043e\u0440), \u043c\u044b \u043d\u0435 \u0441\u043c\u043e\u0436\u0435\u043c \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c (\u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c) \u043d\u043e\u0432\u044b\u0439 JWT \u0442\u043e\u043a\u0435\u043d \u0441 \u0438\u0445 \u043f\u043e\u043c\u043e\u0449\u044c\u044e. \u0418\u0442\u0430\u043a, \u043c\u044b \u0432\u044b\u0437\u043e\u0432\u0435\u043c \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0435\u0442 <code>cookie<\/code> \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442 \u0438\u0445, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0432\u0435\u0440\u043d\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0435\u0441\u043b\u0438 \u043e\u043d\u0438 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442. \u042d\u0442\u043e\u0442 \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442 \u0442\u0430\u043a\u0436\u0435 \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0441\u0435\u0430\u043d\u0441\u044b \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f (<code>refreshing sessions<\/code>). \u042d\u0442\u043e\u0442 \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f <code>\/api\/auth.ts<\/code>.<\/p>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c <code>cookie<\/code>, \u0438 \u0435\u0441\u043b\u0438 \u043e\u043d\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u044b &#8212; \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u0415\u0441\u043b\u0438 \u043e\u043d\u0438 \u043d\u0435\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u044b, \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>verify()<\/code> \u0432\u044b\u0434\u0430\u0441\u0442 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0435.<\/p>\n<pre><code class=\"typescript\">export const get: RequestHandler = async (event) => {     const { token, refresh_token } = cookie.parse(event.request.headers.get('cookie') || '');     try {         const user = jwt.verify(token, key) as Record&lt;any, any>;         return {             status: 200,             body: user         };     } catch {         \/\/ invalid or expired token     } }<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0441\u0440\u043e\u043a \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f JWT \u0442\u043e\u043a\u0435\u043d\u0430 \u0438\u0441\u0442\u0435\u043a, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c refresh \u0442\u043e\u043a\u0435\u043d \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0442\u043e\u043a\u0435\u043d\u0430 \u0432 \u043d\u0430\u0448\u0435\u0439 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. \u0415\u0441\u043b\u0438 \u043e\u043d\u0438 \u0440\u0430\u0432\u043d\u044b, \u0442\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 JWT \u0442\u043e\u043a\u0435\u043d.<\/p>\n<pre><code class=\"typescript\">if (!refresh_token) return returnError(401, 'Unauthorized user'); const getUser = await admin.from('users').select().eq(\"refresh_token\", refresh_token).maybeSingle() if (!getUser.data) {     \/\/ remove invalid refresh token     return {         status: 401,         headers: {             'set-cookie': [                 `refresh_token=; Max-Age=0; Path=\/;${secure} HttpOnly`             ]         },     } } const user_data = getUser.data as Users_Table; const new_user = {     username: user_data.username,     user_id: user_data.user_id,     email: user_data.email }; const token = jwt.sign(new_user, key, { expiresIn: `${15 * 60 * 1000}` }); return {     status: 200,     headers: {         'set-cookie': [             `token=${token}; Max-Age=${15 * 60}; Path=\/;${secure} HttpOnly`         ]     }, };<\/code><\/pre>\n<h3>VI. \u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439<\/h3>\n<p>\u0427\u0442\u043e\u0431\u044b \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0443 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0438\u0437 <code>\/api\/auth<\/code> \u0432 <code>load<\/code> \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<\/p>\n<pre><code class=\"typescript\">\/\/ index.sve;te \/\/ inside &lt;script context=\"module\" lang=\"ts\"\/> export const load: Load = async (input) => {     const response = await input.fetch('\/api\/auth');     const user = (await response.json()) as Session;     if (!user.user_id) {         \/\/ user doesn't exist         return {             status: 302,             redirect: '\/signin'         };     }     return {         props: {             user         }     }; };<\/code><\/pre>\n<h3>VII. \u0412\u044b\u0445\u043e\u0434 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u0437 \u0441\u0438\u0441\u0442\u0435\u043c\u044b<\/h3>\n<p>\u0427\u0442\u043e\u0431\u044b \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0441\u0438\u0441\u0442\u0435\u043c\u044b, \u043f\u0440\u043e\u0441\u0442\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u0435 JWT \u0442\u043e\u043a\u0435\u043d \u0438 refresh \u0442\u043e\u043a\u0435\u043d.<\/p>\n<pre><code class=\"typescript\">\/\/ \/api\/signout.ts export const post : RequestHandler =<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-335204","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/335204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=335204"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/335204\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=335204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=335204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=335204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}