Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Alba María Álvarez
/
front_recipes
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
078371d9
authored
Aug 26, 2025
by
Alba María Álvarez
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
feat(authStore): creado almacenamiento para los datos de autenticación de un usuario
parent
1e11b8bf
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
61 additions
and
0 deletions
src/stores/authStore.js
src/stores/authStore.js
0 → 100644
View file @
078371d9
import
{
defineStore
}
from
'pinia'
;
import
api
from
"@/services/api"
;
import
{
authService
}
from
"@/services/auth"
;
export
const
useAuthStore
=
defineStore
(
'auth'
,
{
state
:
()
=>
({
user
:
null
,
token
:
localStorage
.
getItem
(
'jwt_token'
)
||
null
,
isAuthenticated
:
!!
localStorage
.
getItem
(
'jwt_token'
),
}),
actions
:
{
async
login
(
credentials
)
{
try
{
const
{
token
,
email
,
role
}
=
await
authService
.
login
(
credentials
);
this
.
token
=
token
;
this
.
user
=
{
email
,
role
};
this
.
isAuthenticated
=
true
;
localStorage
.
setItem
(
'jwt_token'
,
token
);
localStorage
.
setItem
(
'user_email'
,
email
);
localStorage
.
setItem
(
'user_role'
,
role
);
api
.
defaults
.
headers
.
common
[
'Authorization'
]
=
`Bearer
${
token
}
`
return
true
;
}
catch
(
error
)
{
console
.
error
(
'Error al iniciar sesión:'
,
error
);
let
errorMsg
=
'Error'
;
if
(
error
.
response
&&
error
.
response
.
data
&&
error
.
response
.
data
.
message
)
{
errorMsg
=
error
.
response
.
data
.
message
;
}
throw
new
Error
(
errorMsg
);
}
},
logout
()
{
this
.
token
=
null
;
this
.
user
=
null
;
this
.
isAuthenticated
=
false
;
localStorage
.
removeItem
(
'jwt_token'
);
localStorage
.
removeItem
(
'user_email'
);
localStorage
.
removeItem
(
'user_role'
);
delete
api
.
defaults
.
headers
.
common
[
'Authorization'
];
},
initializeAuth
()
{
const
token
=
localStorage
.
getItem
(
'jwt_token'
);
const
email
=
localStorage
.
getItem
(
'user_email'
);
const
role
=
localStorage
.
getItem
(
'user_role'
);
if
(
token
&&
email
&&
role
)
{
this
.
token
=
token
;
this
.
user
=
{
email
,
role
};
this
.
isAuthenticated
=
true
;
api
.
defaults
.
headers
.
common
[
'Authorization'
]
=
`Bearer
${
token
}
`
;
}
}
}
});
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment