joysofcode / sveltekit-auth-cookies Goto Github PK
View Code? Open in Web Editor NEW๐ช SvelteKit authentication using cookies
๐ช SvelteKit authentication using cookies
Now that version 1.0 is stable and released this would be lovely to have with the latest version of Sveltekit.
I know this was requested before in #2, but would be lovely if time is available. Gave it a try without success.
Hi,
following your Project Setup on https://github.com/joysofcode/sveltekit-auth-cookies
I get this error when trying to register a new user (test/test1)
` โ Local: http://localhost:4173/
โ Network: use --host to expose
Error:
Invalid prisma.user.create() invocation:
An operation failed because it depends on one or more records that were required but not found. No 'Roles' record(s) (needed to inline the relation on 'User' record(s)) was found for a nested connect on one-to-many relation 'RolesToUser'.
at RequestHandler.handleRequestError (D:\work\nodejs\sveltekit-auth-cookies\node_modules@prisma\client\runtime\index.js:30851:13)
at RequestHandler.request (D:\work\nodejs\sveltekit-auth-cookies\node_modules@prisma\client\runtime\index.js:30834:12)
at async PrismaClient._request (D:\work\nodejs\sveltekit-auth-cookies\node_modules@prisma\client\runtime\index.js:31812:16)
at async register (file:///D:/work/nodejs/sveltekit-auth-cookies/.svelte-kit/output/server/entries/pages/(auth)/register/_page.server.ts.js:22:3)
at async handle_action_json_request (file:///D:/work/nodejs/sveltekit-auth-cookies/.svelte-kit/output/server/index.js:332:18)
at async resolve (file:///D:/work/nodejs/sveltekit-auth-cookies/.svelte-kit/output/server/index.js:1984:22)
at async Object.handle (file:///D:/work/nodejs/sveltekit-auth-cookies/.svelte-kit/output/server/chunks/hooks.server.js:5:12)
at async respond (file:///D:/work/nodejs/sveltekit-auth-cookies/.svelte-kit/output/server/index.js:2022:22)
at async file:///D:/work/nodejs/sveltekit-auth-cookies/node_modules/@sveltejs/kit/src/exports/vite/preview/index.js:145:5
Error:
Invalid prisma.user.create()
invocation:`
hi,
could you please add the possibility to assign a registered user a defined role?
eg.:
Admin
User
thx
I just going to through this and recently sveltekit made a change in hooks to now they have added hooks for client too. So for server hooks now file name is hooks.server.ts
.
Hooks Documentation you can get more info from here.
Thanks a lot for this example!
After the installation process, i tried to register myself (yes, i added the user-roles) and got an internal server error
with the response invalid request body
.
After a short research, i found this:
#sveltejs/kit#5370
And updated the sveltejs/kit
in the package.json
from .504
to the current version .560
and run the installation again.
Now it works
Is it possible to make an example that connects to supabase instead of prism to set hooks and session. I've tried to adjust but all to no avail
Steps to reproduce:
The server emits this error, when running npm run dev
, and no page is loaded:
Function called outside component initialization
Error: Function called outside component initialization
at get_current_component (/node_modules/svelte/internal/index.mjs:953:15)
at Module.setContext (/node_modules/svelte/internal/index.mjs:985:5)
at root.svelte:14:1
at $$render (/node_modules/svelte/internal/index.mjs?v=39878779:1770:22)
at Object.render (/node_modules/svelte/internal/index.mjs?v=39878779:1778:26)
at render_response (file:///Users/john/Code/JoysOfCode/sveltekit-auth-cookies/.svelte-kit/runtime/server/index.js:1283:27)
at async respond_with_error (file:///Users/john/Code/JoysOfCode/sveltekit-auth-cookies/.svelte-kit/runtime/server/index.js:2663:10)
at async respond$1 (file:///Users/john/Code/JoysOfCode/sveltekit-auth-cookies/.svelte-kit/runtime/server/index.js:2918:4)
at async resolve (file:///Users/john/Code/JoysOfCode/sveltekit-auth-cookies/.svelte-kit/runtime/server/index.js:3303:11)
at async Object.handle (/Users/john/Code/JoysOfCode/sveltekit-auth-cookies/src/hooks.ts:14:11)
```
I run on Mac:
Node version: v18.5.0
npm version: 8.12.1
Issue: Unable to write to SQLite database
When trying to register a user in my SvelteKit app with Prisma and SQLite, I receive the following error:
Invalid `prisma.user.create()` invocation:
Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind:
QueryError(SqliteFailure(Error { code: ReadOnly, extended_code: 8 },
Some("attempt to write a readonly database"))), transient: false })
I have already tried giving write permissions to both the database and the folder, but the error persists.
Steps to reproduce:
Try to register a new user in the SvelteKit app
Observe the error in the console
Expected behavior:
The user should be successfully registered and added to the SQLite database.
Code
/register/+page.server.ts looks like:
import { fail, redirect } from '@sveltejs/kit'
import bcrypt from 'bcrypt'
import db from '../../../lib/userDatabase';
// using an enum for user roles to avoid typos
// if you're not using TypeScript use an object
enum Roles {
ADMIN = 'ADMIN',
USER = 'USER',
}
export const load = async ({ locals }) => {
// redirect user if logged in
if (locals.user) {
throw redirect(302, '/')
}
}
export const actions = {
register: async ({ request }) => {
const data = await request.formData()
const username = data.get('username')
const password = data.get('password')
console.log("request: " + request);
if (
typeof username !== 'string' ||
typeof password !== 'string' ||
!username ||
!password
) {
return fail(400, { invalid: true })
}
const user = await db.user.findUnique({
where: { username },
})
if (user) {
return fail(400, { user: true })
}
await db.user.create({
data: {
username,
passwordHash: await bcrypt.hash(password, 10),
userAuthToken: crypto.randomUUID(),
role: { connect: { name: Roles.USER } },
},
})
console.log("data: " + data);
throw redirect(303, '/login')
},
}
Environment:
OS: EndeavourOS 6.2.9
NodeJS: 19.8.1
SQLite: 3.41.2
Prisma: 4.12.0
Sveltekit: 1.15.2
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.