React Native auth package for Laravel Sanctum integration
npm install react-native-laravel-sanctumReact Native auth package for Laravel Sanctum integration. While this package is mainly build for Laravel Sanctum APIs, you can use this library for other API-Backends as well.
- Features
- Installation
- Basic Usage
- Example App
- Documentation
- Contributing
- License
- [x] Login
- [x] Logout
- [x] Get current user
- [x] Check if user is authenticated
- [x] CSRF-Token
``sh`
npm install react-native-laravel-sanctumexpo-secure-store$3
As the package relies on you will need to create a new build of your app to use it.
Wrap your app with AuthProvider and pass the config object as a prop.
`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',
};
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 (
value={email}
placeholder='Email'
/>
value={password}
secureTextEntry
placeholder='Password'
/>
);
}
``
You can find the example app here.
You can find the documentation here.
See the contributing guide to learn how to contribute to the repository and the development workflow.
MIT