2.0.1 • Published 9 months ago

@disruptph/react-auth-context v2.0.1

Weekly downloads
7
License
MIT
Repository
github
Last release
9 months ago

@disruptph/react-auth-context

Generic react auth context

Usage

Typescript/React Example

import React, { useState } from 'react';
import { AuthProvider, AuthService, useAuth } from '@disruptph/react-auth-context';

class MyAuthService implements AuthService {
  async loginWithEmail(email: string, password: string) {
    // Handle email/password login via custom backend api, firebase, or anything you prefer
  }

  async checkAuth() {
    // Handle auth check
  }

  async logout() {
    // Handle logout
  }
}

const App = () => (
  <AuthProvider service={new MyAuthService()}>
    <Login />
  </AuthProvider>
);

const Login = () => {
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');
  const { login } = useAuth();

  const handleSubmit = async () => {
    await login(email, password);
  }

  return (
    <form onSubmit={handleSubmit}>
      <input value={email} onChange={e => setEmail(e.target.value)} />
      <input value={password} onChange={e => setPassword(e.target.value)} type="password" />
    </form>
  );
};
2.0.1

9 months ago

2.0.0

9 months ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.0

3 years ago