The auth hook for react
$ yarn add wing-use-auth
# or
$ npm install wing-use-auth
import useAuth, { AuthProvider } from "wing-use-auth";
// App.js
import { AuthProvider } from "wing-use-auth";
export default function App() {
return (
<AuthProvider options={{ cookieKey: "my-app-auth-key-name" }}>
<ExamplePage />
</AuthProvider>
);
}
// ExamplePage.js
import useAuth from "wing-use-auth";
function ExamplePage() {
const auth = useAuth();
const login = () => {
auth.signin(() => {
// do something here
// such as: set storage/cookie
console.log("Logged in");
});
};
return (
<div>
{auth.authenticated ? (
<h1>You have authenticated</h1>
) : (
<div>
You have not logged in yet,<button onClick={login}>Log In</button>
</div>
)}
</div>
);
}
Attribute | Version | Remark |
---|---|---|
initialState |
<=0.0.3 | deprecated |
cookieKey |
>=0.0.4 | 根据 cookie 值来设置初识的状态 (例如: 在登录的时候设置 cookie 的 |