Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add react support (jsx and tsx) #36

Merged
merged 5 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 InertiaJS
fouteox marked this conversation as resolved.
Show resolved Hide resolved

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
Expand All @@ -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';

<LoginLink v-if="$page.props.environment == 'local'" />
<LoginLink v-if="$page.props.environment === 'local'" />

// or

<LoginLink v-if="$page.props.environment == 'local'" label="Login as user@example.com" class="pb-3 text-red-500" :redirect-url="route('dashboard')" />
<LoginLink v-if="$page.props.environment === 'local'" label="Login as user@example.com" class="pb-3 text-red-500" :redirect-url="route('dashboard')" />
```

### 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' && (
<LoginLink />
)}

// or

{page.props.environment === 'local' && (
<LoginLink
label="Login as user@example.com"
className="pb-3 text-red-500"
redirect-url="route('dashboard')"
fouteox marked this conversation as resolved.
Show resolved Hide resolved
/>
)}
```

### 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.

Expand Down
33 changes: 33 additions & 0 deletions resources/js/LoginLink.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react';
import { router } from '@inertiajs/react'

const LoginLink = ({
fouteox marked this conversation as resolved.
Show resolved Hide resolved
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 (
<form onSubmit={submit}>
<button className={className} type="submit">
{label}
</button>
</form>
);
};

export default LoginLink;
43 changes: 43 additions & 0 deletions resources/ts/LoginLink.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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<string, any>;
}

const LoginLink: React.FC<LoginLinkProps> = ({
fouteox marked this conversation as resolved.
Show resolved Hide resolved
className = 'underline',
email = null,
guard = null,
keyId = null,
label = 'Login',
redirectUrl = null,
userAttributes = null,
}) => {
const submit = (event: FormEvent) => {
fouteox marked this conversation as resolved.
Show resolved Hide resolved
event.preventDefault();
router.post(route('loginLinkLogin'), {
email: email,
key: keyId,
redirect_url: redirectUrl,
guard: guard,
user_attributes: userAttributes,
});
};

return (
<form onSubmit={submit}>
<button className={className} type="submit">
{label}
</button>
</form>
);
};

export default LoginLink;