Skip to content

Commit c1a93dd

Browse files
authored
Merge pull request #41 from dgraph-io/apoorv/support-old-login
Support old login method
2 parents c926784 + dcef71c commit c1a93dd

File tree

5 files changed

+86
-3
lines changed

5 files changed

+86
-3
lines changed

lib/client.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export declare class DgraphClient {
1212
setAlphaAuthToken(authToken: string): void;
1313
setSlashApiKey(apiKey: string): void;
1414
login(userid: string, password: string): Promise<boolean>;
15+
loginIntoNamespace(userid: string, password: string, namespace?: number): Promise<boolean>;
1516
logout(): void;
1617
newTxn(options?: TxnOptions): Txn;
1718
setDebugMode(mode?: boolean): void;

lib/client.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ var DgraphClient = (function () {
8888
});
8989
});
9090
};
91+
DgraphClient.prototype.loginIntoNamespace = function (userid, password, namespace) {
92+
return __awaiter(this, void 0, void 0, function () {
93+
var c;
94+
return __generator(this, function (_a) {
95+
this.debug("Login request:\nuserid: " + userid);
96+
c = this.anyClient();
97+
return [2, c.loginIntoNamespace(userid, password, namespace)];
98+
});
99+
});
100+
};
91101
DgraphClient.prototype.logout = function () {
92102
this.debug("Logout");
93103
this.clients.forEach(function (c) { return c.logout(); });

lib/clientStub.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,35 @@ var DgraphClientStub = (function () {
261261
};
262262
DgraphClientStub.prototype.login = function (userid, password, refreshToken) {
263263
return __awaiter(this, void 0, void 0, function () {
264+
var body, res;
264265
return __generator(this, function (_a) {
265-
return [2, this.loginIntoNamespace(userid, password, 0, refreshToken)];
266+
switch (_a.label) {
267+
case 0:
268+
if (this.legacyApi) {
269+
throw new Error("Pre v1.1 clients do not support Login methods");
270+
}
271+
body = {};
272+
if (userid === undefined &&
273+
refreshToken === undefined &&
274+
this.refreshToken === undefined) {
275+
throw new Error("Cannot find login details: neither userid/password nor refresh token are specified");
276+
}
277+
if (userid === undefined) {
278+
body.refresh_token =
279+
refreshToken !== undefined ? refreshToken : this.refreshToken;
280+
}
281+
else {
282+
body.userid = userid;
283+
body.password = password;
284+
}
285+
return [4, this.callAPI("login", __assign(__assign({}, this.options), { method: "POST", body: JSON.stringify(body) }))];
286+
case 1:
287+
res = _a.sent();
288+
this.accessToken = res.data.accessJWT;
289+
this.refreshToken = res.data.refreshJWT;
290+
this.maybeStartRefreshTimer(this.accessToken);
291+
return [2, true];
292+
}
266293
});
267294
});
268295
};

src/client.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ export class DgraphClient {
7373
return c.login(userid, password); // tslint:disable-line no-unsafe-any
7474
}
7575

76+
/**
77+
* loginIntoNamespace obtains access tokens from Dgraph Server for the particular userid & namespace
78+
*/
79+
public async loginIntoNamespace(
80+
userid: string,
81+
password: string,
82+
namespace?: number,
83+
): Promise<boolean> {
84+
this.debug(`Login request:\nuserid: ${userid}`);
85+
86+
const c = this.anyClient();
87+
return c.loginIntoNamespace(userid, password, namespace); // tslint:disable-line no-unsafe-any
88+
}
89+
7690
/**
7791
* logout - forget all access tokens.
7892
*/

src/clientStub.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,38 @@ export class DgraphClientStub {
296296
password?: string,
297297
refreshToken?: string,
298298
): Promise<boolean> {
299-
return this.loginIntoNamespace(userid, password, 0, refreshToken);
299+
if (this.legacyApi) {
300+
throw new Error("Pre v1.1 clients do not support Login methods");
301+
}
302+
303+
const body: { [k: string]: string } = {};
304+
if (
305+
userid === undefined &&
306+
refreshToken === undefined &&
307+
this.refreshToken === undefined
308+
) {
309+
throw new Error(
310+
"Cannot find login details: neither userid/password nor refresh token are specified",
311+
);
312+
}
313+
if (userid === undefined) {
314+
body.refresh_token =
315+
refreshToken !== undefined ? refreshToken : this.refreshToken;
316+
} else {
317+
body.userid = userid;
318+
body.password = password;
319+
}
320+
321+
const res: LoginResponse = await this.callAPI("login", {
322+
...this.options,
323+
method: "POST",
324+
body: JSON.stringify(body),
325+
});
326+
this.accessToken = res.data.accessJWT;
327+
this.refreshToken = res.data.refreshJWT;
328+
329+
this.maybeStartRefreshTimer(this.accessToken);
330+
return true;
300331
}
301332

302333
public async loginIntoNamespace(
@@ -309,7 +340,7 @@ export class DgraphClientStub {
309340
throw new Error("Pre v1.1 clients do not support Login methods");
310341
}
311342

312-
const body: { [k: string ]: string | number} = {};
343+
const body: { [k: string]: string | number } = {};
313344
if (
314345
userid === undefined &&
315346
refreshToken === undefined &&

0 commit comments

Comments
 (0)