Skip to content

Commit

Permalink
finding my way around
Browse files Browse the repository at this point in the history
  • Loading branch information
BasWilson committed Nov 8, 2021
1 parent ab498ee commit 3ab23bb
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 35 deletions.
11 changes: 10 additions & 1 deletion api/config/functions/astroplant.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const axios = require("axios");
const jwtDecode = require("jwt-decode");
const DATA_API_URL = "https://api.astroplant.sda-projects.nl";

const formatError = error => [
{ messages: [{ id: error.id, message: error.message, field: error.field }] }
];
Expand Down Expand Up @@ -54,13 +55,16 @@ module.exports = {
*/
async login(ctx, params) {
try {
strapi.log.info("Loggin in user on AstroPlant Core API...");
const res = await axios.post(DATA_API_URL + "/me/auth", {
username: params.identifier,
password: params.password
});

strapi.log.info("Logged in to AstroPlant Core API...");

// getting the infos from the request to the AstroPlant Core API.
const json = await res.data;
const json = res.data;

const refreshToken = json.refreshToken;
const accessToken = json.accessToken;
Expand All @@ -76,6 +80,9 @@ module.exports = {
accessToken: json.accessToken
};
} catch (err) {

strapi.log.error("Failed to log in to AstroPlant Core API");

return ctx.badRequest(
null,
formatError({
Expand All @@ -95,6 +102,7 @@ module.exports = {

// If the user doesn't exist on the AstroPlant Core API then he is created there
try {
console.log("CREATING USER ON CORE API", params)
const res = await axios.post(DATA_API_URL + "/users", {
username: params.username,
emailAddress: params.email,
Expand All @@ -117,6 +125,7 @@ module.exports = {
// else we return an error

strapi.log.error("Could not create user on AstroPlant Core API.");
console.log("CORE API ERROR", err)

return ctx.badRequest(
null,
Expand Down
17 changes: 16 additions & 1 deletion api/extensions/users-permissions/config/routes.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@
"actionType": "update"
}
}
},
{
"method": "POST",
"path": "/auth/local/register",
"handler": "User.create",
"config": {
"policies": [],
"prefix": "",
"description": "Create a user in Strapi and the AstroPlant Core API",
"tag": {
"plugin": "users-permissions",
"name": "User",
"actionType": "create"
}
}
}
]
}
}
2 changes: 2 additions & 0 deletions api/extensions/users-permissions/controllers/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = {
* @return {Object}
*/
async create(ctx) {
console.log("CREATING USER")
const advanced = await strapi
.store({
environment: "",
Expand Down Expand Up @@ -86,6 +87,7 @@ module.exports = {
}

try {
console.log("CRETATING USER", username)
// Try to create the user on the data api if it doesn't already exist
await strapi.config.functions.astroplant.signup(ctx, {
username: username,
Expand Down
71 changes: 38 additions & 33 deletions api/package-lock.json

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

0 comments on commit 3ab23bb

Please sign in to comment.