Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions .replit
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
compile = "./.config/build.sh"
run = "node --enable-source-maps tests/index.test.js"
entrypoint = "index.ts"
run = "npm run test"
entrypoint = "src/index.ts"

[languages.typescript]
pattern = "**/{*.ts,*.js,*.tsx,*.jsx}"
Expand All @@ -19,9 +18,52 @@ guessImports = true

[env]
XDG_CONFIG_HOME = "/home/runner/.config"
PATH = "/home/runner/$REPL_SLUG/.config/npm/node_global/bin:/home/runner/$REPL_SLUG/node_modules/.bin"
npm_config_prefix = "/home/runner/$REPL_SLUG/.config/npm/node_global"

[nix]
channel = "stable-21_11"

[gitHubImport]
requiredFiles = [".replit", "replit.nix", ".config"]

[debugger]
support = true

[debugger.interactive]
transport = "localhost:0"
startCommand = ["dap-node"]

[debugger.interactive.initializeMessage]
command = "initialize"
type = "request"

[debugger.interactive.initializeMessage.arguments]
clientID = "replit"
clientName = "replit.com"
columnsStartAt1 = true
linesStartAt1 = true
locale = "en-us"
pathFormat = "path"
supportsInvalidatedEvent = true
supportsProgressReporting = true
supportsRunInTerminalRequest = true
supportsVariablePaging = true
supportsVariableType = true

[debugger.interactive.launchMessage]
command = "launch"
type = "request"

[debugger.interactive.launchMessage.arguments]
console = "externalTerminal"
cwd = "."
pauseForSourceMap = false
program = "./index.js"
request = "launch"
sourceMaps = true
stopOnEntry = false
type = "pwa-node"

[unitTest]
language = "nodejs"
5 changes: 5 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Copyright 2022 Replit

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Repl Authentication is a simple way to allow users to log in, and access their i
Ensure that a user is logged in with replit following [our docs](https://docs.replit.com/hosting/authenticating-users-repl-auth)
```js
const express = require('express');
const { getUserInfo } = require('../index.js')
const { getUserInfo } = require('@replit/repl-auth')

const app = express();
app.use(express.static('public'));
Expand All @@ -18,16 +18,19 @@ app.get('/', async function(req, res) {

## Docs

> `getUserInfo(Request req)`
> `getUserInfo(RequestLike request)`

Gets all user info. Returns UserInfo object.
Gets all user info. Returns a UserInfo object if the user is logged in.
```js
const userInfo = getUserInfo(req)
```

UserInfo object:
```
UserInfo {
UserInfo type object:
```ts
// You can import it:
import type { UserInfo } from "@replit/repl-auth"

interface UserInfo {
id?: string;
name?: string;
bio?: string;
Expand All @@ -43,4 +46,4 @@ UserInfo {
```sh
npm run test
```
Then login and assure that it says "server tests passed"
Then log in (not on localhost) and assure that it says `server tests passed`
11 changes: 7 additions & 4 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Request } from 'express';
interface UserInfo {
export interface RequestLike {
headers: {
[key: string]: string;
};
}
export interface UserInfo {
id?: string;
name?: string;
bio?: string;
Expand All @@ -8,5 +12,4 @@ interface UserInfo {
roles?: Array<string>;
teams?: Array<string>;
}
export declare const getUserInfo: (req: Request) => UserInfo | null;
export {};
export declare const getUserInfo: (req: RequestLike) => UserInfo | null;
50 changes: 26 additions & 24 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getUserInfo = void 0;
//remove header prefix and convert to camel case
//doesn't affect the value
function cleanHeader(headerName) {
return headerName.replace("x-replit-user-", "").replace(/-(.)/g, function(_, group1) {
return group1.toUpperCase();
});
return headerName.replace("x-replit-user-", "").replace(/-(.)/g, function (_, group1) {
return group1.toUpperCase();
});
}
var getUserInfo = function(req) {
var headers = req.headers;
var userInfo = {};
for (var _i = 0, _a = Object.keys(headers); _i < _a.length; _i++) {
var headerName = _a[_i];
var headerValue = headers[headerName];
if (headerName.startsWith("x-replit-") && headerValue) {
var cleanHeaderName = cleanHeader(headerName);
//check if property is meant to be an array
if (cleanHeaderName === 'roles' || cleanHeaderName === 'teams') {
userInfo[cleanHeaderName] = headerValue.split(',');
}
else {
userInfo[cleanHeaderName] = headerValue;
}
var getUserInfo = function (req) {
var headers = req.headers;
var userInfo = {};
for (var _i = 0, _a = Object.keys(headers); _i < _a.length; _i++) {
var headerName = _a[_i];
var headerValue = headers[headerName];
if (headerName.startsWith("x-replit-") && headerValue && typeof headerValue === 'string') {
var cleanHeaderName = cleanHeader(headerName);
//check if property is meant to be an array
if (cleanHeaderName === 'roles' || cleanHeaderName === 'teams') {
userInfo[cleanHeaderName] = (headerValue).split(',');
}
else {
userInfo[cleanHeaderName] = headerValue;
}
}
}
}
//check if userInfo is empty
if (Object.keys(userInfo).length === 0 && userInfo.constructor === Object) {
return null;
}
return userInfo;
//check if userInfo is empty
if (Object.keys(userInfo).length === 0 && userInfo.constructor === Object) {
return null;
}
return userInfo;
};
exports.getUserInfo = getUserInfo;
137 changes: 0 additions & 137 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,5 @@
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@types/express": "^4.17.13"
}
"license": "ISC"
}
Loading