diff --git a/README.md b/README.md index ea16163..1320006 100644 --- a/README.md +++ b/README.md @@ -175,9 +175,9 @@ If the user that needs to be logged in does not exist, the package will use the If you don't want this behaviour, set `automatically_create_missing_users` in the `local-link` config file to `false`. -### Usage with Vue and InertiaJS +### Usage with Vue or React and Inertia.js -The package has a built-in component to support Vue and InertiaJS. The props are the same of blade component. +The package has a built-in component to support Vue or React and InertiaJS. The props are the same of blade component. Edit the `HandleInertiaRequests` middleware like so: ```php @@ -190,21 +190,43 @@ public function share(Request $request): array } ``` -So, if you need to show the button only in your local environment, use the component like so: +So, if you need to show the button only in your local environment, use the component like so for Vue : ```vue import LoginLink from '@/../../vendor/spatie/laravel-login-link/resources/js/login-link.vue'; - + // or - + ``` -### Usage with React / Js / ... +For React, use the component like so: -The package comes with Vue support only. When you use any other JS front end framework to render your views, you can still make use of the package. +```jsx +import LoginLink from '@/../../vendor/spatie/laravel-login-link/resources/js/LoginLink'; +// or for TypeScript, uncomment the following line +//import LoginLink from '@/../../vendor/spatie/laravel-login-link/resources/ts/LoginLink'; + +{page.props.environment === 'local' && ( + +)} + +// or + +{page.props.environment === 'local' && ( + +)} +``` + +### Usage with Js / ... + +The package comes with Vue and React support only. When you use any other JS front end framework to render your views, you can still make use of the package. You should send a `POST` request to `/laravel-login-link-login`. If you don't give it any payload, then it will log in the first user in your users table. If there is no user, it will be created. diff --git a/resources/js/LoginLink.jsx b/resources/js/LoginLink.jsx new file mode 100644 index 0000000..f7bbbfa --- /dev/null +++ b/resources/js/LoginLink.jsx @@ -0,0 +1,31 @@ +import React from 'react'; +import { router } from '@inertiajs/react' + +export default function LoginLink({ + className = 'underline', + email = null, + guard = null, + keyId = null, + label = 'Login', + redirectUrl = null, + userAttributes = null, +}) { + const submit = (event) => { + event.preventDefault(); + router.post(route('loginLinkLogin'), { + email: email, + key: keyId, + redirect_url: redirectUrl, + guard: guard, + user_attributes: userAttributes, + }); + }; + + return ( +
+ +
+ ); +}; diff --git a/resources/ts/LoginLink.tsx b/resources/ts/LoginLink.tsx new file mode 100644 index 0000000..b305e18 --- /dev/null +++ b/resources/ts/LoginLink.tsx @@ -0,0 +1,41 @@ +import React, { FormEvent } from 'react'; +import { router } from '@inertiajs/react' + +interface LoginLinkProps { + className?: string; + email?: string; + guard?: string; + keyId?: string; + label?: string; + redirectUrl?: string; + userAttributes?: Record; +} + +export default function LoginLink({ + className = 'underline', + email = null, + guard = null, + keyId = null, + label = 'Login', + redirectUrl = null, + userAttributes = null, +}: LoginLinkProps) { + function submit(event: FormEvent) { + event.preventDefault(); + router.post(route('loginLinkLogin'), { + email: email, + key: keyId, + redirect_url: redirectUrl, + guard: guard, + user_attributes: userAttributes, + }); + }; + + return ( +
+ +
+ ); +};