-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |