Skip to content

Instantly share code, notes, and snippets.

@dmi3coder
Created June 5, 2020 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmi3coder/50450ce866e4f9ef7142692bbbba4104 to your computer and use it in GitHub Desktop.
Save dmi3coder/50450ce866e4f9ef7142692bbbba4104 to your computer and use it in GitHub Desktop.
Example of Auth class for Quarkus and React Swagger communication
import React, {useState} from 'react';
import Networking from "./Networking";
export default function Auth({authCallback, ...props}) {
const [info, setInfo] = useState(null);
const [login, setLogin] = useState("test");//pre-fill user data for usability
const [password, setPassword] = useState("test");
const [email, setEmail] = useState("test@mail.com");
const [isRegistering, setRegistering] = useState(false)
const register = () => {
Networking.exec({
endpoint: client => client.apis.default.register,
data: {requestBody: {name: login, email: email, password: password /* never do this in production */}},
success: res => {
setInfo("You've registered successfully, now login")
},
failure: err => {
console.log(err)
}
})
}
const authenticate = () => {
Networking.exec({
endpoint: client => client.apis.default.login,
attributes: {login: login, password: password},
success: res => {
localStorage.setItem("id_token", res.data); // store token in local storage
authCallback()
},
failure: res => {
setInfo(res)
}
})
}
return <div>
<h1>{isRegistering ? 'Register' : 'Login'}</h1>
Register:<input type="checkbox" onChange={(e) => {
setRegistering(!isRegistering)
}}/>
{info && <div>{info}</div>}
<div>
Login:
<input name="Login" value={login} onChange={(e) => setLogin(e.target.value)}/>
</div>
{isRegistering && <div>
Email:
<input name="Email" value={email} onChange={(e) => setEmail(e.target.value)}/>
</div>}
<div>
Password:
<input type="password" value={password} name="Password" onChange={(e) => setPassword(e.target.value)}/>
</div>
<button onClick={isRegistering ? register : authenticate}>Submit</button>
</div>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment