Maintenant que nous avons notre première page suite à Workshop 1 : Création d’une landing page pour chef d’oeuvre, nous pouvons ajouter une page d’enregistrement d’utilisateur!
C’est un bon prétexte pour ajouter un backend + base de donnée.
psql -U postgres -W postgres
(si ça ne fonctionne pas, sudo -u postgres psql postgres
)CREATE DATABASE synthesizrs;
spring.datasource.name=database
spring.datasource.url=jdbc:postgresql://localhost:5432/synthesizrs
spring.datasource.username=postgres
spring.datasource.password=password
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL95Dialect
spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop
spring.jpa.generate-ddl=true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
./gradlew build
npx express-generator server-node
npm install
npm install sequelize pg pg-hstore body-parser cors --save
module.exports = {
HOST: "localhost",
USER: "postgres",
PASSWORD: "password",
DB: "synthesizrs",
dialect: "postgres"
};
const dbConfig = require("../config/db.config.js");
const Sequelize = require("sequelize");
const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, {
host: dbConfig.HOST,
dialect: dbConfig.dialect,
operatorsAliases: false,
});
const db = {};
db.Sequelize = Sequelize;
db.sequelize = sequelize;
db.models = require("./model.js")(sequelize, Sequelize);
module.exports = db;
// database
const db = require("./models");
db.sequelize.sync({ force: true }).then(() => {
console.log("Drop and re-sync db.");
});
npm install
# ...
# Executing (default): SELECT 1+1 AS result
# Drop and re-sync db.
/register
(Java / Spring Boot)config
, créer une classe pour initialiser la sécurité:
@Configuration
@EnableWebSecurity
public class BasicAuthConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(passwordEncoder());
}
@Override
protected void configure(HttpSecurity security) throws Exception {
security
// disable form login security (for login)
.csrf().disable()
// requests URL
.authorizeRequests()
.antMatchers("/api/**").authenticated()
// authentication type
.and().httpBasic();
}
@Bean
public PasswordEncoder passwordEncoder() {
return NoOpPasswordEncoder.getInstance();
}
@Override
@Bean(name = BeanIds.AUTHENTICATION_MANAGER)
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}
controllers
, créer une classe pour votre point d’entré (endpoint) pour le front
@CrossOrigin
@RestController
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/register")
public void register(@RequestBody UserDto user) {
userService.registerUser(new User(user.getUsername(), user.getPassword()));
}
}
model
, créer une classe User
, et sa classe associée UserDto
and le package dtos
:
@Entity(name = "users")
public class User {
@Id
private String username;
@Column
private String password;
}
repositories
, créer UserRepository
services
, créer UserService
et UserServiceImpl
{
"username": "moi",
"password": "password"
}
/register
(Node.js / Express) module.exports = (sequelize, Sequelize) => {
return sequelize.define("user", {
username: {
type: Sequelize.STRING
},
password: {
type: Sequelize.STRING
},
});
};
db.models = require("./model.js")(sequelize, Sequelize);
{"username":"test","password":"test"}
Rendez-vous à l’atelier suivant, Workshop 3 : Création d’une page de login (ajout authentification), pour ajouter une page de login.