Commit 48a9a246a691a15897a18c5bf32fe64bf8be3957

Authored by Apichat.Tum
1 parent d360cefc
Exists in master

fix auth

.idea/misc.xml 0 → 100644
... ... @@ -0,0 +1,16 @@
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<project version="4">
  3 + <component name="JavaScriptSettings">
  4 + <option name="languageLevel" value="ES6" />
  5 + </component>
  6 + <component name="ProjectLevelVcsManager" settingsEditedManually="false">
  7 + <OptionsSetting value="true" id="Add" />
  8 + <OptionsSetting value="true" id="Remove" />
  9 + <OptionsSetting value="true" id="Checkout" />
  10 + <OptionsSetting value="true" id="Update" />
  11 + <OptionsSetting value="true" id="Status" />
  12 + <OptionsSetting value="true" id="Edit" />
  13 + <ConfirmationsSetting value="0" id="Add" />
  14 + <ConfirmationsSetting value="0" id="Remove" />
  15 + </component>
  16 +</project>
0 17 \ No newline at end of file
... ...
.idea/modules.xml 0 → 100644
... ... @@ -0,0 +1,8 @@
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<project version="4">
  3 + <component name="ProjectModuleManager">
  4 + <modules>
  5 + <module fileurl="file://$PROJECT_DIR$/.idea/smart-rms-calendar.iml" filepath="$PROJECT_DIR$/.idea/smart-rms-calendar.iml" />
  6 + </modules>
  7 + </component>
  8 +</project>
0 9 \ No newline at end of file
... ...
.idea/smart-rms-calendar.iml 0 → 100644
... ... @@ -0,0 +1,12 @@
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<module type="WEB_MODULE" version="4">
  3 + <component name="NewModuleRootManager">
  4 + <content url="file://$MODULE_DIR$">
  5 + <excludeFolder url="file://$MODULE_DIR$/.tmp" />
  6 + <excludeFolder url="file://$MODULE_DIR$/temp" />
  7 + <excludeFolder url="file://$MODULE_DIR$/tmp" />
  8 + </content>
  9 + <orderEntry type="inheritedJdk" />
  10 + <orderEntry type="sourceFolder" forTests="false" />
  11 + </component>
  12 +</module>
0 13 \ No newline at end of file
... ...
.idea/watcherTasks.xml 0 → 100644
... ... @@ -0,0 +1,4 @@
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<project version="4">
  3 + <component name="ProjectTasksOptions" suppressed-tasks="Babel" />
  4 +</project>
0 5 \ No newline at end of file
... ...
app/controllers/home.controller.js
... ... @@ -166,7 +166,7 @@ function oauth2callback(req, res) {
166 166 res.send(err)
167 167 res.end()
168 168 } else {
169   - res.redirect('http://localhost:8000/calendar')
  169 + res.redirect('http://localhost:3030/home')
170 170 }
171 171 })
172 172 }
... ...
app/lib/index.js
... ... @@ -62,7 +62,7 @@ function storeToken(token, emails) {
62 62 throw err;
63 63 }
64 64 // ** new
65   - Promise.try(function () { })
  65 + Promise.try(function () {})
66 66 .then(function () {
67 67 tokendb.save(function (err, result) {
68 68 if (err) {
... ... @@ -91,18 +91,19 @@ module.exports = {
91 91 var auth = new googleAuth();
92 92 var oauth2Client = new auth.OAuth2(clientId, clientSecret, REDIRECTURL);
93 93 var setToken = {} // set token from callback
94   - Token.findOne({ email: 'tzbattleboy@gmail.com' }, function (err, token) {
95   -
  94 + Token.findOne({
  95 + email: 'tzbattleboy@gmail.com'
  96 + }, function (err, token) {
96 97 if (err) {
97 98 return callback(null, null, getNewToken(oauth2Client, callback));
98 99 } else if (token) {
99 100 setToken = {
100   - access_token: token.access_token,
101   - refresh_token: token.refresh_token,
102   - token_type: token.token_type,
103   - expiry_date: token.expiry_date
104   - }
105   - // console.log(setToken)
  101 + access_token: token.access_token,
  102 + refresh_token: token.refresh_token,
  103 + token_type: token.token_type,
  104 + expiry_date: token.expiry_date
  105 + }
  106 + // console.log(setToken)
106 107 oauth2Client.credentials = setToken;
107 108 return callback(null, oauth2Client);
108 109 } else {
... ... @@ -127,19 +128,28 @@ module.exports = {
127 128 var auth = new googleAuth();
128 129 var oauth2Client = new auth.OAuth2(clientId, clientSecret, REDIRECTURL);
129 130  
130   - fs.readFile(TOKEN_PATH, (err, token) => {
131   - if (err) {
  131 + Token.findOne({
  132 + email: 'tzbattleboy@gmail.com'
  133 + }, function (err, token) {
  134 + if (token) {
  135 + oauth2Client.credentials = JSON.parse(token);
  136 + return callback(null, oauth2Client);
  137 + } else {
132 138 oauth2Client.getToken(code, function (err, token) {
133 139 if (err) {
134 140 console.log('Error while trying to retrieve access token', err);
135 141 return;
136 142 } else {
137 143 oauth2Client.credentials = token;
138   - var params = { userId: 'me', fields: 'emails', auth: oauth2Client };
  144 + var params = {
  145 + userId: 'me',
  146 + fields: 'emails',
  147 + auth: oauth2Client
  148 + };
139 149  
140 150 plus.people.get(params, function (err, response) {
141 151 if (err) {
142   - consol.error(err)
  152 + console.error(err)
143 153 } else {
144 154 storeToken(token, response.emails);
145 155 }
... ... @@ -149,11 +159,16 @@ module.exports = {
149 159 }
150 160  
151 161 })
152   - } else {
153   - oauth2Client.credentials = JSON.parse(token);
154   - return callback(null, oauth2Client);
155 162 }
156 163 });
  164 +
  165 + // fs.readFile(TOKEN_PATH, (err, token) => {
  166 + // if (err) {
  167 +
  168 + // } else {
  169 +
  170 + // }
  171 + // });
157 172 });
158 173 },
159 174  
... ...