Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
S
shop-api-js5
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vadim
shop-api-js5
Commits
a14bfaa3
Commit
a14bfaa3
authored
Feb 01, 2021
by
Vadim
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
конец занятия №91
parent
ac05badd
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
62 additions
and
0 deletions
+62
-0
config.js
app/config.js
+4
-0
User.js
app/models/User.js
+4
-0
users.js
app/users.js
+40
-0
package-lock.json
package-lock.json
+13
-0
package.json
package.json
+1
-0
No files found.
app/config.js
View file @
a14bfaa3
...
...
@@ -7,5 +7,9 @@ module.exports = {
db
:
{
name
:
"shop"
,
url
:
"mongodb://localhost"
},
facebook
:
{
appId
:
'175634290592255'
,
secret
:
'8904c56e5400c12237cce6f85e1cefe0'
}
};
\ No newline at end of file
app/models/User.js
View file @
a14bfaa3
...
...
@@ -36,6 +36,10 @@ const UserSchema = new Schema({
required
:
true
,
enum
:
[
'user'
,
'admin'
],
default
:
'user'
},
facebookId
:
{
type
:
Number
,
minlength
:
10
}
});
...
...
app/users.js
View file @
a14bfaa3
const
express
=
require
(
"express"
);
const
User
=
require
(
"./models/User"
);
const
auth
=
require
(
"./middleware/auth"
);
const
config
=
require
(
'./config'
);
const
{
nanoid
}
=
require
(
'nanoid'
);
const
axios
=
require
(
'axios'
);
const
router
=
express
.
Router
();
...
...
@@ -49,6 +52,43 @@ const createRouter = () => {
return
res
.
send
(
success
);
});
router
.
post
(
'/facebookLogin'
,
async
(
req
,
res
)
=>
{
const
inputToken
=
req
.
body
.
accessToken
;
const
accessToken
=
`
${
config
.
facebook
.
appId
}
|
${
config
.
facebook
.
secret
}
`
;
const
tokenUrl
=
`https://graph.facebook.com/debug_token?input_token=
${
inputToken
}
&access_token=
${
accessToken
}
`
;
try
{
const
response
=
await
axios
.
get
(
tokenUrl
);
if
(
response
.
data
.
error
)
{
return
res
.
status
(
401
).
send
({
message
:
'Facebook token error'
});
}
if
(
response
.
data
.
data
.
user_id
!==
req
.
body
.
userID
)
{
return
res
.
status
(
401
).
send
({
message
:
'Wrong User ID'
});
}
let
user
=
await
User
.
findOne
({
facebookId
:
req
.
body
.
id
});
if
(
!
user
)
{
user
=
new
User
({
username
:
req
.
body
.
email
,
email
:
req
.
body
.
email
,
password
:
nanoid
(),
facebookId
:
req
.
body
.
id
})
}
user
.
generateToken
();
await
user
.
save
({
validateBeforeSave
:
false
});
return
res
.
send
({
message
:
'Login or register successful'
,
user
});
}
catch
(
e
)
{
return
res
.
status
(
401
).
send
({
message
:
'Facebook token error'
,
e
});
}
});
return
router
;
};
...
...
package-lock.json
View file @
a14bfaa3
...
...
@@ -155,6 +155,14 @@
"resolved"
:
"https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz"
,
"integrity"
:
"sha1-ml9pkFGx5wczKPKgCJaLZOopVdI="
},
"axios"
:
{
"version"
:
"0.21.1"
,
"resolved"
:
"https://registry.npmjs.org/axios/-/axios-0.21.1.tgz"
,
"integrity"
:
"sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA=="
,
"requires"
:
{
"follow-redirects"
:
"^1.10.0"
}
},
"balanced-match"
:
{
"version"
:
"1.0.0"
,
"resolved"
:
"https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz"
,
...
...
@@ -760,6 +768,11 @@
}
}
},
"follow-redirects"
:
{
"version"
:
"1.13.2"
,
"resolved"
:
"https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.2.tgz"
,
"integrity"
:
"sha512-6mPTgLxYm3r6Bkkg0vNM0HTjfGrOEtsfbhagQvbxDEsEkpNhw582upBaoRZylzen6krEmxXJgt9Ju6HiI4O7BA=="
},
"forwarded"
:
{
"version"
:
"0.1.2"
,
"resolved"
:
"https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz"
,
...
...
package.json
View file @
a14bfaa3
...
...
@@ -11,6 +11,7 @@
"author"
:
""
,
"license"
:
"
ISC
"
,
"dependencies"
:
{
"
axios
"
:
"^0.21.1"
,
"
bcrypt
"
:
"^5.0.0"
,
"
cors
"
:
"^2.8.5"
,
"
express
"
:
"^4.17.1"
,
...
...
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