Skip to content

Commit

Permalink
Create auth.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 7, 2024
1 parent 970f2f3 commit ba471ab
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions projects/PiWalletBot/client/context/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import jwt from 'jsonwebtoken';

const auth = {
async login(username, password) {
try {
const response = await api.post('/login', { username, password });
const token = response.data.token;
localStorage.setItem('token', token);
return token;
} catch (error) {
throw error;
}
},

async register(username, password) {
try {
const response = await api.post('/register', { username, password });
const token = response.data.token;
localStorage.setItem('token', token);
return token;
} catch (error) {
throw error;
}
},

async verifyToken(token) {
try {
const decoded = jwt.verify(token, process.env.SECRET_KEY);
return decoded;
} catch (error) {
return null;
}
},

async logout() {
localStorage.removeItem('token');
},
};

export default auth;

0 comments on commit ba471ab

Please sign in to comment.