https://github.com/tschucki/react-native-laravel-sanctum
A simplified package for effortless user authentication in React Native using Laravel Sanctum.
https://github.com/tschucki/react-native-laravel-sanctum
expo laravel laravel-sanctum react-native react-native-library typescript
Last synced: 2 months ago
JSON representation
A simplified package for effortless user authentication in React Native using Laravel Sanctum.
- Host: GitHub
- URL: https://github.com/tschucki/react-native-laravel-sanctum
- Owner: Tschucki
- License: mit
- Created: 2023-08-13T12:10:30.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-02-16T17:52:49.000Z (4 months ago)
- Last Synced: 2025-03-28T18:09:21.582Z (3 months ago)
- Topics: expo, laravel, laravel-sanctum, react-native, react-native-library, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/react-native-laravel-sanctum
- Size: 1.13 MB
- Stars: 21
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# React Native Laravel Sanctum
React Native auth package for [Laravel Sanctum ](https://laravel.com/docs/10.x/sanctum) integration. While this package is mainly build for Laravel Sanctum APIs, you can use this library for other API-Backends as well.
## Table of Contents
- [Features](#features)
- [Installation](#installation)
- [Basic Usage](#basic-usage)
- [Example App](#example-app)
- [Documentation](#documentation)
- [Contributing](#contributing)
- [License](#license)## Features
- [x] Login
- [x] Logout
- [x] Get current user
- [x] Check if user is authenticated
- [x] CSRF-Token## Installation
```sh
npm install react-native-laravel-sanctum
```
### Install native dependencies
As the package relies on `expo-secure-store` you will need to create a new build of your app to use it.## Basic Usage
Wrap your app with `AuthProvider` and pass the `config` object as a prop.
Make sure your validation rules, within your controller look like this:
```php
[
'email' => 'required|email',
'password' => 'required',
'deviceName' => 'required',
];
``````js
import { AuthProvider } from 'react-native-laravel-sanctum';export default function App() {
const config = {
loginUrl: 'https://your-awesome-domain/api/sanctum/token',
logoutUrl: 'https://your-awesome-domain/api/logout',
userUrl: 'https://your-awesome-domain/api/user',
csrfTokenUrl: 'https://your-awesome-domain/sanctum/csrf-cookie'
};return (
...
);
}
```You can now use the `useAuth` hook within the AuthProvider to access the auth methods.
```js
import { useAuth } from 'react-native-laravel-sanctum';export default function Login() {
const { login, getToken, updateUser, setUserIsAuthenticated, isAuthenticated, logout, currentUser } = useAuth();
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');const handleLogin = async () => {
const tokenObtained = await login(email, password, 'Test-Device');
if (tokenObtained) {
const user = await updateUser();if (user.id) {
setUserIsAuthenticated(true);
}
}
};return (
You are not logged in.
setEmail(text)}
value={email}
placeholder='Email'
/>
setPassword(text)}
value={password}
secureTextEntry
placeholder='Password'
/>
Login
);
}
```## Example App
You can find the example app [here](https://github.com/Tschucki/react-native-laravel-sanctum-example-app).
## Documentation
You can find the documentation [here](https://github.com/Tschucki/react-native-laravel-sanctum/wiki).
## Contributing
See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
## License
MIT